文件无法使用 PHP fopen 打开
我已经尝试过:
<?php
$fileip = fopen("test.txt","r");
?>
这应该以只读方式打开文件,但事实并非如此 test.txt 文件与 index.php (主项目文件夹)位于同一文件夹中,
该文件无法打开
,当我输入 echo 时:
echo $fileip;
它返回
资源 id #3
i have tried this:
<?php
$fileip = fopen("test.txt","r");
?>
this should have opened the file in read only mood but it doesn't
the test.txt file is in same folder as that of index.php (main project folder)
the file doesn't open
and when i put echo like :
echo $fileip;
it returned
Resource id #3
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(6)
文件确实打开得很好,您不能像那样回显它,因为它是文件指针,而不是文件本身的内容。您需要使用
fread()
来读取实际内容,或者更好的是,使用file_get_contents()
立即获取内容。按照您的方式进行:
或者使用
file_get_contents()
:The file did open just fine, you cannot echo it like that because it's a file pointer, not the contents of the file itself. You need to use
fread()
to read the actual contents, or better yet, usefile_get_contents()
the get the content straight away.Doing it your way:
Or, using
file_get_contents()
:来自 php.net:
由于返回了
资源
,您的文件已成功打开,您需要对文件进行进一步的操作,例如fwrite
等。所以没有错误,文件是可以操作的。From php.net:
Since a
resource
was returned, your file has successfully opened, you need further operations such asfwrite
, etc on your file. So there is no error, the file is there to be manipulated.如果 fopen 调用的结果是得到一个资源 id,那么它就成功了,因为如果失败它将返回 FALSE。那么到底是什么让您怀疑该文件是否确实打开了呢?
查看 http://www.php.net/fopen 了解更多信息。
If you get a resource id as result of the fopen call, then it succeeded, because it will return FALSE if it fails. So what exactly makes you doubt that the file is actually open?
Check http://www.php.net/fopen for more information.
您只打开了文件句柄,而不是文件本身。
如果您使用的是 PHP5 - 您确实应该用于新开发,您可以使用 $fileip = file_get_contents("test.txt") 来将该文件的内容读入缓冲区。
You've only opened a file handle, not the file itself.
If you're using PHP5 - which you really should be for new development, you could instead use $fileip = file_get_contents("test.txt") which will read the contents of this file into the buffer.
一个完整的例子。
A complete example.
输出文本文件内容:
To output the text file contents: