简体   繁体   English

使用 php 写入文件时检查行是否受影响

[英]Check if line affected when writing a file with php

how can i check if a line was written to a file?如何检查一行是否写入文件? I am trying something like this:我正在尝试这样的事情:

    if (fwrite($handle, $data) == FALSE) {
    echo "<script>
            alert('Not written');
        </script>";
    include ('index.html');
    }
    else{
    echo "<script>
            alert('Written');
        </script>";
            include ('index2.html');}
    fclose($handle);

but it still notifies me as written even if nothing was put in the file.但即使文件中没有任何内容,它仍会通知我已写入。

check your parameters first (as markus suggested).首先检查您的参数(如 markus 建议的那样)。

Also check comparison operator.还要检查比较运算符。 You should use === instead of == in if statement.您应该在 if 语句中使用 === 而不是 ==。

Try file_put_contents , it returns number of bytes written, or false on failure.尝试file_put_contents ,它返回写入的字节数,或者失败时返回 false。

if (file_put_contents($filename, $data, FILE_APPEND) !== false) {
  // success
} else {
  // fail: function failure
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM