检查文本文件是否为空

时间:2017-12-08 11:20:50

标签: php explode

我想知道如何检查文本文件是否为空。这意味着即使有一些空间也没有文字,即它是空白的

function keyRemain($path)
{
    $ambil = file_get_contents("data/$path/keywords.txt");
    $kw = explode(",", $ambil);
    if (count($kw) > 1) {
        return false;
    } else {
        return true;
    }
}

3 个答案:

答案 0 :(得分:4)

您必须检查空函数以及trim

function keyRemain($path)
{
    $ambil = trim(file_get_contents("data/$path/keywords.txt"));
    var_dump($ambil); // check the output here
    if(!empty($ambil)) {
        return false;
    } else {
        return true;
    }
}

答案 1 :(得分:1)

也许这不是答案,只是另一种检查文件的方法。在此之前,代码出现在类中。在我将它剪切并将其移到课堂之外后,它可以完美地工作而没有任何错误。

答案 2 :(得分:-1)

  

file_get_contents()将读取整个文件,而filesize()使用stat()来取消文件大小。使用filesize(),它应该消耗更少的磁盘I / O.

在堆栈上找到here的答案......

你也可以(在同一个链接上有这个答案):

clearstatcache();
if(filesize($path_to_your_file)) {
    // your file is not empty
}