且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何检查文本文件是否在PHP中为空?

更新时间:2023-11-29 09:12:52

'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.