php 警告 feof() 期望参数 1 是资源
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/16552484/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
warning feof() expects parameter 1 to be resource
提问by Jeff
My error logs are getting out of control with the two below errors
我的错误日志因以下两个错误而失控
warning feof() expects parameter 1 to be resource
and
和
warning fread() expects parameter 1 to be resource
The bit of code responsible is
负责的代码位是
<?php
$file = '../upload/files/' . $filex;
header("Content-Disposition: attachment; filename=" . urlencode($file));
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Description: File Transfer");
header("Content-Length: " . filesize($file));
flush(); // this doesn't really matter.
$fp = fopen($file, "r");
while (!feof($fp)) {
echo fread($fp, 65536);
flush(); // this is essential for large downloads
}
fclose($fp);
?>
I used this code for header downloads but its freaking out right now - before anyone asks what I have tried, I tried google but still don't fully understand the error message.
我使用此代码进行标题下载,但它现在吓坏了 - 在有人问我尝试过什么之前,我尝试了谷歌,但仍然没有完全理解错误消息。
回答by ChristopheBrun
fopen fails and returns false. false is not a resource, thus the warning.
fopen 失败并返回 false。false 不是资源,因此是警告。
You'd better test $fp before injecting it as a resource-like argument:
在将 $fp 作为类似资源的参数注入之前,您最好对其进行测试:
if(($fp = fopen($file, "r"))) {
[...]
}
回答by user2916217
I recently encountered this problem. The code ran perfectly on my local environment. But when it was loaded to the server, I got the message discussed in this thread. At the end, the problem was that the server is case-sensitive on file names, while my local environment is not. After having corrected the file name, everything started to work.
我最近遇到了这个问题。代码在我的本地环境中完美运行。但是当它被加载到服务器时,我得到了这个线程中讨论的消息。最后,问题是服务器对文件名区分大小写,而我的本地环境不是。更正文件名后,一切开始工作。
回答by Stephen Hodgkiss
From PHP 7, you can now use the far more efficient method: -
从 PHP 7 开始,您现在可以使用更高效的方法:-
$fileRecs = 0;
$file = new SplFileObject('textfile.txt, 'r');
$file->seek(PHP_INT_MAX);
$fileRecs = $File->key() + 1;
echo "fileRecs=".$fileRecs;
echo "fileRecs=".$fileRecs;