如何检查文本文件是否在PHP中为空? [英] How to check if a text file is empty in PHP?
问题描述
如何检查PHP中的文本文件是否为空?
我尝试了在互联网上发现的内容:
if(''!= filesize('data.txt')){
echoThe file is empty;
$ b $也是这样:
$ b 如果(0!= filesize('data.txt')){
echo文件是空的; pre>
}
其中没有一个可以工作。
'data.txt')== 0){
echoThe file is DEFINITELY empty;
$ b
如果还有疑问(取决于空的含义你也可以尝试:
pre $ if $(trim(file_get_contents('data.txt'))== false){
回显文件也是空的;
$ / code>
请注意Niels Keurentjes,因为 http://php.net/manual/en/function.empty.php 说:
在PHP 5.5之前,empty()只支持变量;任何其他的
都会导致一个解析错误。换句话说,以下内容将不起作用:
empty(trim($ name))。相反,使用trim($ name)== false。
How to check if a text file is empty in PHP?
I've tried what I found on the internet which is this:
if( '' != filesize('data.txt')){
echo "The file is empty";
}
ALSO THIS:
if( 0 != filesize('data.txt')){
echo "The file is empty";
}
And no of them seems to work.
Simply use this first:
if (filesize('data.txt') == 0){
echo "The file is DEFINITELY empty";
}
if still in doubt (depending what empty meaning to you), also try:
if (trim(file_get_contents('data.txt')) == false) {
echo "The file is empty too";
}
Take a note on Niels Keurentjes, becareful as http://php.net/manual/en/function.empty.php said:
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中为空?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!