3

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 />\n";

    // delete empty files
}
JasonDavis
  • 48,204
  • 100
  • 318
  • 537
  • 2
    and the problem is? Check http://php.net/manual/en/function.unlink.php – Robert Sep 16 '15 at 19:42
  • @Robert When I get to each file in my text file I have the file path to the file but I need help checking too see if it is an empty file and if it is then I need to delete the file. If not empty ignore and check the next file – JasonDavis Sep 16 '15 at 19:43
  • @JasonDavis Please update your question – Michel Sep 16 '15 at 19:45

2 Answers2

2

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 />\n";

    if (!file_exists($file)) {
        echo "file " . $file_label . " does not exist<br />\n";
    } else if (filesize($file) === 0) {
        echo "deleting file: " . $file_label . "<br />\n";
        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 \r and \n characters from each line returned =]

newfurniturey
  • 37,556
  • 9
  • 94
  • 102
  • Little hint: OP probably wants to add the flag: `FILE_IGNORE_NEW_LINES` to the `file()` call, since otherwise he will get the file size of: `xy.php\n` – Rizier123 Sep 16 '15 at 19:46
  • This won't work in every case. `filesize()` can return FALSE and you don't use `===` operator. and `false == 0 `is true. – Robert Sep 16 '15 at 19:48
  • @JasonDavis You're welcome. I already thought that this will cause a problem :] – Rizier123 Sep 16 '15 at 20:00
1

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);
    }
}
Robert
  • 19,800
  • 5
  • 55
  • 85
  • Looks good. The size check is what I needed help with so I tried `echo $file . ': ' . filesize($file) . ' bytes
    ';` however it returns empty value for each file based on a path like this `/lib/Zend/Tag/Cloud/start.php` any ideas
    – JasonDavis Sep 16 '15 at 19:49
  • it may not be empty but false which is different. filesize() will return false if there are errors while reading file, for example permissions. You need also make cure that path is correct which can be done with file_exists() – Robert Sep 16 '15 at 19:50
  • 1
    Issue was the missing `FILE_IGNORE_NEW_LINES` flag in `files()` – JasonDavis Sep 16 '15 at 20:04