fwrite() 没有写入任何内容,空白条目

发布于 2024-10-07 13:21:29 字数 839 浏览 4 评论 0原文

$is_file_1$is_file_2 都是 false,但没有任何内容插入到我设置的 errlog-Batch.txt 文件中。我不确定我做错了什么,因为没有输出脚本错误

$dirchk1 = "/temp/files/" . $ch_id . "/" . $data[0];
$dirchk2 = "/temp/files/" . $ch_id . "/" . $data[1];

$is_file_1 = is_file($dirchk1);
$is_file_2 = is_file($dirchk2);

$missing_n == 'file does not exist : ' . '".$data[0]."' . "\r";
$missing_s == 'file does not exist : ' . '".$data[1]."' . "\r";
$renfile_n == 'file can not be move file : ' . '".$data[0]."' . "\r";
$renfile_s == 'file can not be move file : ' . '".$data[1]."' . "\r";   

$handle = @fopen("/rec/" . "errlog-" . $batchid . ".txt", "a");
if($is_file_1 == FALSE) {
    fwrite($handle, $missing_n . "\n");
} elseif ($is_file_2 == FALSE) {
    fwrite($handle, $missing_s . "\n");
}
@fclose($handle);
//exit();

$is_file_1 and $is_file_2 are both false yet nothing is inserted into the errlog-Batch.txt file that I set. I'm not sure what i'm doing wrong since no script error is outputted

$dirchk1 = "/temp/files/" . $ch_id . "/" . $data[0];
$dirchk2 = "/temp/files/" . $ch_id . "/" . $data[1];

$is_file_1 = is_file($dirchk1);
$is_file_2 = is_file($dirchk2);

$missing_n == 'file does not exist : ' . '".$data[0]."' . "\r";
$missing_s == 'file does not exist : ' . '".$data[1]."' . "\r";
$renfile_n == 'file can not be move file : ' . '".$data[0]."' . "\r";
$renfile_s == 'file can not be move file : ' . '".$data[1]."' . "\r";   

$handle = @fopen("/rec/" . "errlog-" . $batchid . ".txt", "a");
if($is_file_1 == FALSE) {
    fwrite($handle, $missing_n . "\n");
} elseif ($is_file_2 == FALSE) {
    fwrite($handle, $missing_s . "\n");
}
@fclose($handle);
//exit();

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

北座城市 2024-10-14 13:21:30

您在以下位置使用 == 代替 =

$missing_n == 'file does not exist : ' . '".$data[0]."' . "\r";
$missing_s == 'file does not exist : ' . '".$data[1]."' . "\r";
$renfile_n == 'file can not be move file : ' . '".$data[0]."' . "\r";
$renfile_s == 'file can not be move file : ' . '".$data[1]."' . "\r";

因此,四个变量仍然未定义。当您尝试将它们写入附加换行符的文件时,您看到的只是换行符。这是假设您的 fopen 成功。

在继续执行 file-io 之前,必须始终检查 fopen 的返回值。

You are using == in place of = in:

$missing_n == 'file does not exist : ' . '".$data[0]."' . "\r";
$missing_s == 'file does not exist : ' . '".$data[1]."' . "\r";
$renfile_n == 'file can not be move file : ' . '".$data[0]."' . "\r";
$renfile_s == 'file can not be move file : ' . '".$data[1]."' . "\r";

As a result the four variables remain undefined. And when you try to write them to the file appended with a newline, all you see is a newline. This is assuming your fopen succeeds.

You must always check the return value of fopen before you go ahead and do file-io.

少年亿悲伤 2024-10-14 13:21:30

除了使用错误的运算符之外,您还可以使用 error_log 代替您自己的错误日志功能:

$dirchk1 = "/temp/files/" . $ch_id . "/" . $data[0];
$dirchk2 = "/temp/files/" . $ch_id . "/" . $data[1];

if (!is_file($dirchk1)) {
    error_log(sprintf('file does not exist: "%s"', $data[0]), 3, "/rec/errlog-$batchid.txt");
}
if (!is_file($dirchk2)) {
    error_log(sprintf('file does not exist: "%s"', $data[1]), 3, "/rec/errlog-$batchid.txt");
}

Besides the usage of the wrong operator, you could also use error_log instead of your own error log function:

$dirchk1 = "/temp/files/" . $ch_id . "/" . $data[0];
$dirchk2 = "/temp/files/" . $ch_id . "/" . $data[1];

if (!is_file($dirchk1)) {
    error_log(sprintf('file does not exist: "%s"', $data[0]), 3, "/rec/errlog-$batchid.txt");
}
if (!is_file($dirchk2)) {
    error_log(sprintf('file does not exist: "%s"', $data[1]), 3, "/rec/errlog-$batchid.txt");
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文