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

如何在PHP中检查文本文件是否为空?,php,file,Php,File,如何在PHP中检查文本文件是否为空 我尝试了我在互联网上找到的东西,这是: if( '' != filesize('data.txt')){ echo "The file is empty"; } 还包括: if( 0 != filesize('data.txt')){ echo "The file is empty"; } 它们似乎都不起作用。应该是这样。检查文件大小是否为0。代码询问您文件大小是否与0不同,然后它是空的 if ( 0 == filesize( $fil

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

我尝试了我在互联网上找到的东西,这是:

if( '' != filesize('data.txt')){

    echo "The file is empty";
}
还包括:

if( 0 != filesize('data.txt')){

    echo "The file is empty";
}

它们似乎都不起作用。

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

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

首先,相等是
=
运算符-您现在正在执行反向检查。但即使如此,空性也不是绝对的,您可能会遇到一个伪空文本文件,它实际上有一个换行符或UTF-8 BOM(字节顺序标记)

if(filesize($path)<16&&empty(trim(file\u get\u contents($path)))
die(“根据定义,这是可行的”);
范例


只需先使用此选项:

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";
}
记下Niels Keurentjes,注意如下所述:

在PHP5.5之前,empty()只支持变量;还有别的吗 导致分析错误。换言之,以下措施将不起作用: 空(修剪($name))。相反,请使用trim($name)==false


我对小xml文件使用file\u get\u内容。以下代码适用于我,这里归功于Lex,并附有解释:


如果文件为空,
filesize
将返回
0
,我们可以将其用作
布尔值,类似于:

$txtFile = "someFile.txt";
if( ! filesize( $txtFile ) )
{
    // EMPTY FILE
}

以下是基于提供的答案的两个选项,用于按大小范围检查内容。根据这些建议,如果满足文件大小,其内容将“包括”在页面上。

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

可以输入文件本身:

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.';
echo'文件存在!'
'; echo“文件有内容!”。'
'; echo“文件为空”; 回显“未找到文件”; echo“文件存在并具有内容!”。'
'; echo“未找到文件或文件为空”;
如果
myfile.txt
将包含高达2Gb的内容,该怎么办?如果这是一个现实的情况,并且仅仅检查一个0大小的文件是不够的,那么您必须实现仅使用
fopen
fread
读取前几个字节。或者添加一个
filesize
检查,它跳过了一些假设。为了完整而编辑。file_get_contents()将读取整个文件,因此它会消耗更多的磁盘I/O。filesize($path)更好。我认为:if(file_get_contents('data.txt')!=“”)并且它也不能使用filesize。我更喜欢file_get_内容,因为我的文本文件很小,大小为5或6 kb,因此它可能不是copmlite空文件,您可以使用此选项的答案,如果指定的文件不存在,则将生成E_警告,因此应将其包装在is_file()调用中
if((!is_file($filename))| |(0===filesize($filename))
如果不是,实际上应该是
。比如
if(!filesize($txtFile))
$txtFile = "someFile.txt";
if( ! filesize( $txtFile ) )
{
    // EMPTY FILE
}
As a string throughout: 'file.txt'
As a variable: $file = 'file.txt';
As a constant (per examples): define ('FILE', 'file.txt');
   <?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
    ?>
<?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.';