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

4
如何在PHP中检查文本文件是否为空?
我找到了网上的方法,如下所示:
if( '' != filesize('data.txt')){

    echo "The file is empty";
}

请提供需要翻译的具体内容,谢谢。
if( 0 != filesize('data.txt')){

    echo "The file is empty";
}

没有一个似乎起作用。
8个回答

13

首先请使用以下方法:

if (filesize('data.txt') == 0){
    echo "The file is DEFINITELY empty";
}

如果还存在疑惑(取决于您对的含义理解),也可以尝试:

if (trim(file_get_contents('data.txt')) == false) {
    echo "The file is empty too";
}

注意尼尔斯·库伦特耶斯的问题,根据http://php.net/manual/en/function.empty.php所说:

在 PHP 5.5 之前,empty() 只支持变量;任何其他内容都将导致解析错误。换句话说,以下内容将不起作用:empty(trim($name))。而应使用 trim($name) == false。


1
如果指定的文件不存在,这将生成一个E_WARNING,因此应该将其包装在is_file()调用中。if((!is_file($filename)) || (0 === filesize($filename))) - GordonM

4

应该是这样的。您检查文件大小是否为0。您的代码询问文件大小是否不为0,然后它为空。

if ( 0 == filesize( $file_path ) )
{
    // file is empty
}

那么它可能不是一个完全空的文件,您可以使用Niels Keurentjes的答案。 - TeeDeJee

4
首先,等于号==表示相等 - 您现在正在进行相反的检查。但即使如此,空并不是绝对的,您可能会遇到一个假空文本文件,其中实际上有一个换行符或UTF-8 BOM(字节顺序标记)。
if(filesize($path) < 16 && empty(trim(file_get_contents($path))) )
     die('This works by definition.');

2
如果 myfile.txt 文件包含多达2GB的内容,会怎样? - BlitZ
1
如果这是一个现实的情况,并且仅检查大小为0的文件不足够,那么您将不得不使用fopenfread来实现仅读取前几个字节。或者添加一个filesize检查以跳过某些假设。编辑只是为了完整起见。 - Niels Keurentjes

1
我使用 file_get_contents 处理小型的 XML 文件。以下代码对我有效,感谢 Lex 的贡献,并附上解释: file_get_contents with empty file not working PHP
if(file_get_contents($file) !== false) {
    echo 'your file is empty';
} else {
    echo 'file is not empty';
}

0
如果文件为空,filesize 将返回 0,我们可以将其用作类似于 boolean 的东西。
$txtFile = "someFile.txt";
if( ! filesize( $txtFile ) )
{
    // EMPTY FILE
}

实际上应该是“IF NOT”。就像if( !filesize( $txtFile ) ) - mayid

0
The filesize() function returns the size of the specified file.

This function returns the file size in bytes on success or FALSE on failure.

例子

<?php
echo filesize("test.txt");
?>

0
if (file_get_contents($file_path) != "") {
    echo 'file is not empty';
} else {
    echo 'file is empty';
}

file_get_contents()将读取整个文件,因此会消耗更多的磁盘I/O。filesize($path)更好。 - Zied R.
我尝试了以下代码:如果 (file_get_contents('data.txt') != "") 但是filesize也不起作用。我更喜欢使用file_get_contents,因为我的文本文件很小,只有5或6 KB。 - Montoolivo

0

这里有几个选项可以在 Niels Keurentjes 提供的答案上进行扩展,以检查内容是否符合大小范围。通过这些建议,如果满足文件大小,其内容将被“包含”在页面中。 http://php.net/manual/en/function.include.php

两者都会检查文件是否存在,然后根据大小(以字节为单位)进行评估。如果文件大小大于或等于 5 字节,则打印成功消息并包含文件内容。如果找不到文件或文件小于 5 字节(有效为空),则显示错误消息并且不包含该文件。在出现错误时,使用 clearstatcache 防止文件结果在页面刷新时仍保留。(我尝试过带/不带 clearstatcache 的函数,它确实有帮助。) http://php.net/manual/en/function.clearstatcache.php

文件本身可以输入:

As a string throughout: 'file.txt'
As a variable: $file = 'file.txt';
As a constant (per examples): define ('FILE', 'file.txt');

选项#1:函数

   <?php
    define ('FILE', 'file.txt');

    function includeFile() {
    if (file_exists(FILE))
    {
    echo 'File exists!' . '<br>';
    if (filesize(FILE) >= 5)
    {
    echo 'File has content!' . '<br>';
    include FILE;
    } else {
    echo 'File is empty.';
    clearstatcache();
    }
    }
    else {
    echo 'File not found.';
    clearstatcache();}
    }
    includeFile(); // call the function
    ?>

选项 #2:If/Else语句

<?php
define ('NEW_FILE', 'newfile.txt');

if (file_exists(NEW_FILE) && (filesize(NEW_FILE)) >= 5){
echo 'File exists and has content!' . '<br>';
include NEW_FILE;
}
else {
echo 'File not found or is empty.';
clearstatcache();
}
?>

可以删除验证:

echo 'File exists!' . '<br>';
echo 'File has content!' . '<br>';
echo 'File is empty.';
echo 'File not found.';
echo 'File exists and has content!' . '<br>';
echo 'File not found or is empty.';

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接