此函数在我的服务器上造成了大麻烦,因为它处于循环中:
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);
}
在我的日志中,我可以看到:"PHP警告:fgets()要求参数1为resource,在第42行/path/file.PHP中给出布尔值"
感兴趣的线路是:
$line = fgets($file_handle);
功能还可以,但我不知道为什么会给我这个奇怪的错误。
因为$file_handle
是布尔型false
(您可以用var_dump
检查这一点),而这反过来又是因为fopen
调用失败。
fopen
成功时返回文件指针资源,出错时返回FALSE。
试试这个:
$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'n";
}
fclose($file_handle);
}
好吧,只有一点建议,为了保护代码的安全,请在尝试打开文件之前执行此测试:
$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.
fopen可能会失败,因为您没有访问该文件的权限,或者您没有匹配路径。
但您正在循环到fgets();这意味着没有文件结尾。
试着放置$line = fgets($file_handle,4096);
,看看它是否有效。