fopen() 不工作

Tim*_*cht 6 php fopen file

我想从大约 3-4 mb 的文本文件中读取一个简单的字符串,但 fopen() 失败(从 die() 调用“无法打开文件”)。这是代码:

clearstatcache();
$fh = fopen("/my/path/to/file.txt", "r") or die("can't open file");
$sql = fread($fh,filesize("/my/path/to/file.txt"));
Run Code Online (Sandbox Code Playgroud)

pb1*_*149 5

您是否首先检查过该文件是否存在?

if (!file_exists("/my/path/to/file.txt") { 
    die('File does not exist');
}

clearstatcache();

$fh = fopen("/my/path/to/file.txt", "r") or die("can't open file");
$sql = fread($fh,filesize("/my/path/to/file.txt"));
Run Code Online (Sandbox Code Playgroud)


You*_*nse 5

你必须将这一行添加到你的代码中

error_reporting(E_ALL);
Run Code Online (Sandbox Code Playgroud)

并始终在所有代码中保留此行

还有这条线

ini_set('display_errors',1);
Run Code Online (Sandbox Code Playgroud)

并将此行仅保留在开发服务器上。
在生产时应改为

ini_set('display_errors',0);
ini_set('log_errors',1);
Run Code Online (Sandbox Code Playgroud)

通过这样做,您将不需要 Stackoverflow 帮助来阅读现在明显的错误消息。