Warning: fread (): length parameter must be greater than 0

I want to create a system with a license with my own code, but it gives me an error

the code is similar to this, but in lic.txt the same is 1234567, what’s wrong?

This is an error if I put @ before fread dosent to show an error, but does not open the file

Warning: fread (): the length parameter must be greater than 0 in / home / u 422978792 / public_html / platforma / license / index.php on line 7 Invalid license key

        <?php



    $fp = fopen("http://platforma.dar-project.org/license/lic.txt", "r");
    stream_set_timeout($fp, 10);
    $license = fread($fp, filesize($filename));
    fclose($fp);

    if ($license == "1234567") {

    echo "Your license key is valid";

    } else {

    die("Invalid license key");
    }

    ?>
+4
source share
6 answers

Before use fread, check the file size if(filesize($my_file) > 0).

For instance:

<?php

$my_file = 'list.txt';
$handle = fopen($my_file, 'r');
$data = '';
if(filesize($my_file) > 0)
    $data = fread($handle,filesize($my_file));
//---
echo $data;
//---
fclose($handle);

?>
+4
source

There is a reason why

Warning: fread (): length parameter must be greater than 0

Your file lic.txtmust be empty. Therefore, fread()it can not read anything from the file.

, , echo 'Invalid license key'.

0

$filename - undefined. :

$filename = "lic.txt";

URL-. .

0

PHP / , (, URL) 0 .

, , "-1", , .

$file_content = ReadtextFile( "file.txt" ); // caller


function ReadtextFile($FileName)
  {
  if (file_exists($FileName) )
    {
    $file_size = filesize($FileName);
    if ($file_size == 0) return "";
    }
  else return "-1";

  if (!$fp = fopen($FileName, "r")) return "-1";

  $s01 = fread($fp, filesize($FileName));
  fclose($fp);

  return $s01;
  }
0

, - OpenCart, . Cpanel .

0

PHP / , , .

<?php
if(file_exists("webdictionary.txt"))
{
$myfile = fopen("webdictionary.txt", "r") or 
die("Unable to open file!");
$len =filesize("webdictionary.txt");
echo "File content lenths:- $len .<br>";
echo "file containt.<br>";
for ($i=0;$i<$len;$i++)
{
echo fgetc($myfile);
}
echo "<br>";
fclose($myfile);
echo "$myfile";
}
else 
{
return -1;  
}

?>
0

All Articles