Hi,
I have the following code that reads all the files in a directory but i do not wish for it to list the directories but i am unsure how to stop it listing the dirs.
$fol = "/var/www/zerobytes/test/";
if ($handle = opendir($fol)) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != ".." && $entry != "removed.txt" && $entry != is_dir($entry)) {
$myfile = fopen("".$fol."removed.txt", "a") or die("Unable to open file!");
$txt = "$entry\n";
fwrite($myfile, $txt);
fclose($myfile);
}
}
closedir($handle);
}
fixed it
$fol = "/var/www/zerobytes/test/";
if ($handle = opendir($fol)) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != ".." && $entry != "removed.txt" && $entry != is_dir("".$fol."".$entry."")) {
//Check filesize
if(filesize("".$fol."".$entry."") < '1000'){
$myfile = fopen("".$fol."removed.txt", "a") or die("Unable to open file!");
$txt = "$entry\r\n";
fwrite($myfile, $txt);
fclose($myfile);
echo "".$entry." - Removed <br>";
unlink ("".$fol."".$entry."");
}
}
}
closedir($handle);
}
echo "If no files are listed above then the files are bigger than 0kb";
Not sure where $txt comes from in your code.
//Narrow the list down to only the files we're interested in.
$files = array_filter(glob($fol), function($filename) { return (!is_dir($filename) && substr($filename,-11) != "removed.txt" && filesize($filename) < 1000); } );
//Operate only on those files.
foreach($files AS $file) {
file_put_contents($fol."removed.txt",$txt,FILE_APPEND);
unlink($file);
}
Alternative to find $files
//Only non-Directories
$files = array_diff(glob($fol), glob($fol,GLOB_ONLYDIR));
//Remove removed.txt; (this line can be combined with the previous one, as array_diff takes multiple arrays!
$files = array_diff($files,array_diff($fol."removed.txt"));
//Only small files.
$files = array_filter($files, function($filename) { return filesize($filename) < 1000; }