具有文件句柄的Php函数循环

This function is causing big trouble on my server because is in loop:

function loadFiles()
{
$email = $_POST["emailp"];
$file_handle = fopen("/tmpphp/dmbigmail.file", "r");
while (!feof($file_handle)) {
$line = fgets($file_handle);
if(stristr($line,$email)){
    $show = trim(str_replace($email,' ',$line));
    //echo $show;
    $parsedata = substr($show,0,11);
    $parselink = substr($show,10);
    $total = $parsedata.'<a href=' . $parselink. ">$parselink</a><br>";
    echo $total;
    }
     }
     fclose($file_handle);
 }

In my log I can see this: "PHP Warning: fgets() expects parameter 1 to be resource, boolean given in /path/file.php on line 42"

The line interested is:

$line = fgets($file_handle);

The function is ok, but I dont know why give me this strange error.

Because $file_handle is boolean false (you can check for this with var_dump), which in turn happens because the fopen call fails.

fopen

Returns a file pointer resource on success, or FALSE on error.

Probably fopen fails because you don't have permissions on that file ,or you're miss matching the path .

But you're looping to fgets(); which means there is no End of file .

Try putting $line = fgets($file_handle,4096); and see if it's working.

try this :

$file_handle = @fopen("/tmpphp/dmbigmail.file", "r");
if ($file_handle) {
    while (($line = fgets($file_handle, 4096)) !== false) {
      if(stristr($line,$email)){
            $show = trim(str_replace($email,' ',$line));
            //echo $show;
            $parsedata = substr($show,0,11);
            $parselink = substr($show,10);
            $total = $parsedata.'<a href=' . $parselink. ">$parselink</a><br>";
            echo $total;
        }
    }
    if (!feof($file_handle)) {
        echo "Error: unexpected fgets() fail
";
    }
    fclose($file_handle);
}

Well, only little advice, to secure your code, perform this test before you try to open the file:

$filepath = "/tmpphp/dmbigmail.file";
if (!file_exists($filepath) || !is_file($filepath)) {
  echo "$filepath not found or it is not a file."; exit; //return; //die();
}
if ($file_handle = fopen($filepath, "r")) {
....etc.