文件无法使用 PHP fopen 打开

发布于 2024-08-25 04:15:45 字数 270 浏览 2 评论 0原文

我已经尝试过:

    <?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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(6

吃颗糖壮壮胆 2024-09-01 04:15:45

文件确实打开得很好,您不能像那样回显它,因为它是文件指针,而不是文件本身的内容。您需要使用 fread() 来读取实际内容,或者更好的是,使用 file_get_contents() 立即获取内容。

按照您的方式进行:

$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;

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.

Doing it your way:

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

echo $fileip;

Or, using file_get_contents():

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

echo $fileip;
烟火散人牵绊 2024-09-01 04:15:45

来自 php.net:

返回文件指针资源
成功,或错误时为 FALSE。

由于返回了资源,您的文件已成功打开,您需要对文件进行进一步的操作,例如fwrite等。所以没有错误,文件是可以操作的。

From php.net:

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

Since a resource was returned, your file has successfully opened, you need further operations such as fwrite, etc on your file. So there is no error, the file is there to be manipulated.

独自←快乐 2024-09-01 04:15:45

如果 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.

笨死的猪 2024-09-01 04:15:45

您只打开了文件句柄,而不是文件本身。

如果您使用的是 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.

盛夏尉蓝 2024-09-01 04:15:45

一个完整的例子。

<?php
    $fileip = file_get_contents("test.txt");

    echo($fileip);
?>

A complete example.

<?php
    $fileip = file_get_contents("test.txt");

    echo($fileip);
?>
终难遇 2024-09-01 04:15:45

输出文本文件内容:

$fh   = fopen('myfile.txt', 'r');
$text = fread($fh, filesize('myfile.txt'));
echo $text;

To output the text file contents:

$fh   = fopen('myfile.txt', 'r');
$text = fread($fh, filesize('myfile.txt'));
echo $text;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文