使用PHP fopen无法打开文件 [英] file doesn't open using PHP fopen

查看:207
本文介绍了使用PHP fopen无法打开文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经尝试过了:

    <?php
$fileip = fopen("test.txt","r");

?>

这应该以只读状态打开文件,但没有打开 test.txt文件与index.php位于同一文件夹(主项目文件夹)

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)

文件无法打开

当我像这样放回声时:

echo $fileip;

它返回了

资源ID#3

推荐答案

文件确实打开得很好,您不能像这样回显它,因为它是文件指针,而不是文件本身的内容.您需要使用fread()读取实际内容,或者更好的是,使用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, use file_get_contents() the get the content straight away.

按自己的方式做:

$handle = fopen("test.txt", "r");
$fileip = fread($handle, filesize($filename));
fclose($handle);

echo $fileip;

或者,使用file_get_contents():

$fileip = file_get_contents("test.txt");

echo $fileip;

这篇关于使用PHP fopen无法打开文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆