hi.
one more try... i wrote the following code. it searches a directory for files, subfolders and files in subfolders. all those it saves into the arrays
$filesInArray // FILES & SUBFILES
$dirsInArray // SUBDIRS
$everythingInArray // FILES, SUBDIRS & SUBFILES
after this it prints all on the screen (files blue, dirs red). in the end i shall delete all the files, subdirs and subfiles. all the files are being deleted perfectly, but the dirs make problems. 🙁 it tells me, that some of them are not empty! but i really think that they ARE! 🙁
no idea, what could be wrong. 🙁
could anybody test it for me?? just copy the code in a php-file, then enter a dir-path and run it.
i disabled the deletion by adding / /, so you can check first what the array prints without risking anything.
<?
// USER INPUT
$folderToOpen = "_uploads"; // ENTER PATH TO SEARCH FOLDER HERE
// SCRIPT OUTPUT
$filesInArray = array(); // ALL FILES ARE SAVED IN THIS ARRAY
$dirsInArray = array(); // ALL DIRS ARE SAVED IN THIS ARRAY
$everythingInArray = array(); // FILES AND DIRS ARE SAVED IN THIS ARRAY
$folderToOpen = realpath($folderToOpen)."\\";
$dirsInArrayTemp = array();
$alreadyOpenedDirs = array();
// THE USER'S DEFINED FOLDER IS BEING READ
$folder = opendir($folderToOpen);
while($currentFile = readdir($folder))
{
if($currentFile != "." && $currentFile != "..")
{
if(is_file($folderToOpen.$currentFile))
array_push($filesInArray,$folderToOpen.$currentFile);
elseif(is_dir($folderToOpen.$currentFile))
{
array_push($dirsInArrayTemp,$folderToOpen.$currentFile);
array_push($dirsInArray,$folderToOpen.$currentFile);
}
}
}
// LOOP: AS LONG AS THERE ARE UNREAD FOLDERS THEY ARE BEING READ IN THE FOLLOWING LINES.
// BEGINS WITH THE FOLDERS OF THE opendir ABOVE, SAVES NEW FOLDERS IN $dirsInArrayTemp AND STORES OPENED DIRS IN $alreadyOpenedDirs.
// IN THE END IT CHECKS IF THERE ARE NEW DIRS IN $dirsInArrayTemp (BY COMPARING WITH $alreadyOpenedDirs).
do
{
foreach($dirsInArrayTemp as $currentFile)
{
if(!in_array("$currentFile\\",$alreadyOpenedDirs))
{
$subDirs = $currentFile."\\";
$folderToOpen = opendir(realpath($currentFile));
while($currentFile = readdir($folderToOpen))
{
if($currentFile != "." && $currentFile != "..")
{
if(is_file($subDirs.$currentFile)) // IF THE NEW ENTRY IS A FILE, THEN PUSH IT INTO $filesInArray.
array_push($filesInArray,$subDirs.$currentFile);
elseif(is_dir($subDirs.$currentFile)) // IF THE NEW ENTRY IS A DIR, THEN PUSH IT INTO $dirsInArray SO IT WILL BE CHECKED AGAIN IN THE LOOP.
// ALSO PUSH IT INTO THE $dirsInArray ARRAY.
{
array_push($dirsInArrayTemp,$subDirs.$currentFile);
array_push($dirsInArray,$subDirs.$currentFile);
}
}
}
array_push($alreadyOpenedDirs,$subDirs.$currentFile);
}
}
// ARE THERE NEW FOLDERS, THAT AREN'T ALREADY STORED IN THE $alreadyOpenedDirs ???
unset($dirsLeftCount);
foreach($dirsInArrayTemp as $temp)
{
if(!in_array("$temp\\",$alreadyOpenedDirs))
$dirsLeftCount++;
}
}while($dirsLeftCount > 0);
// ARRAY $everythingInArray IS BEING CREATED BY PUSHING $filesInArray AND $dirsInArray INTO IT.
foreach($filesInArray as $temp)
array_push($everythingInArray,$temp);
foreach($dirsInArray as $temp)
array_push($everythingInArray,$temp);
// SORTING OF ALL ARRAYS.
rsort($filesInArray);
rsort($dirsInArray);
rsort($everythingInArray);
//////////////////////////////////////////////////////////////////////////////////
// PRINT FILES AND FOLDERS
foreach($everythingInArray as $temp)
{
if(is_file("$temp"))
{
echo("<font color=blue>$temp</font><br>");
$filecount++;
}
elseif(is_dir("$temp"))
{
echo("<font color=red>$temp</font><br>");
$dircount++;
}
}
echo "<B><br>files: $filecount<br>";
echo "dirs: $dircount<br></B>";
//*** UN-COMMENT THE FOLLOWING LINES TO DELETE THE FILES AND FOLDERS!!! ***
/*
// DELETION !!!
foreach($everythingInArray as $temp)
{
if(is_file("$temp"))
{
unlink("$temp");
}
elseif(is_dir("$temp"))
{
rmdir("$temp");
}
}
*/
?>
i work on this problem since 2 days, so i would be very happy if someone could help me here. i'm getting nuts with it because i just can't figure out the problem!!! :mad:
thanks a lot!!
j0sh