PHP如何判断文本文件是否为空?
我试过我在互联网上找到的是这样的:
if( '' != filesize('data.txt')){
echo "The file is empty";
}
还有这个:
if( 0 != filesize('data.txt')){
echo "The file is empty";
}
而且它们似乎都不起作用。
最佳答案
首先简单地使用它:
if (filesize('data.txt') == 0){
echo "The file is DEFINITELY empty";
}
如果仍有疑问(取决于 empty 对您的意义),也可以尝试:
if (trim(file_get_contents('data.txt')) == false) {
echo "The file is empty too";
}
注意 Niels Keurentjes,注意 http://php.net/manual/en/function.empty.php说:
Prior to PHP 5.5, empty() only supports variables; anything else will result in a parse error. In other words, the following will not work: empty(trim($name)). Instead, use trim($name) == false.
关于php - 如何检查 PHP 中的文本文件是否为空?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25638868/