迭代PHP文件数组并删除任何没有内容的文件

I have an array of files with full directory path to each file. I need to iterate my files array and then delete ay of the files that are 0byte/non content inside them.

files.txt

/lib/Zend/Gdata/App/LoggingHttpClientAdapterSocket.php
/lib/Zend/Gdata/App/Extension.php
/lib/Zend/Gdata/App/MediaEntry.php
/lib/Zend/Gdata/App/FeedEntryParent.php
/lib/Zend/Gdata/App/AuthException.php
/lib/Zend/ProgressBar/Adapter.php
/lib/Zend/ProgressBar/alias.php
/lib/Zend/Locale/code.php
/lib/Zend/Server/Reflection/Function/article.php
/lib/Zend/Server/Reflection/ReturnValue.php
/lib/Zend/Server/Reflection.php
/lib/Zend/Dojo/BuildLayer.php
/lib/Zend/Tag/Cloud/start.php
/lib/Zend/Tag/Cloud/user.php
/lib/Zend/Tag/Item.php
/lib/Zend/Tag/Cloud.php
/lib/Zend/Ldap/Filter/Not.php
/lib/Zend/Ldap/Filter/And.php
/lib/Zend/Ldap/Filter/Exception.php
/lib/Zend/Ldap/Node.php
/lib/Zend/Ldap/Exception.php

PHP

// list of files to download
$lines = file('files.txt');

// Loop through our array of files from the files.txt file
foreach ($lines as $line_num =>$file) {
    echo htmlspecialchars($file) . "<br />
";

    // delete empty files
}

Your base loop looks good so far, I think what you'll be interested in next are filesize() and unlink():

$lines = file('files.txt', FILE_IGNORE_NEW_LINES);

foreach ($lines as $line_num => $file) {
    $file_label = htmlspecialchars($file);
    echo $file_label . "<br />
";

    if (!file_exists($file)) {
        echo "file " . $file_label . " does not exist<br />
";
    } else if (filesize($file) === 0) {
        echo "deleting file: " . $file_label . "<br />
";
        unlink($file);
    }
}

Though you should really be careful with this to make sure it only deletes files within specific directories, potentially have a whitelist of files that should never be deleted, etc.

Update A good note from a comment is to use the FILE_IGNORE_NEW_LINES in the file() call to strip and characters from each line returned =]

There are two functions to do it one is filesize() which checks size of file and the other one is file_exists() which checks if file exists. To remove file use unlink() function.

foreach ($lines as $line_num =>$file) {
    if(file_exists($file) && filesize($file) === 0) {
        unlink($file);
    }
}