I am using this peice of code..
$target = 'extracted/' . $name[0];
$scan = scandir($target);
To scan the directory of a folder which is used for zip uploads. I want to be able to find all the folders inside my $target
folder so i can delete them and there contents, leaving only the files in the $target directory.
Once i have returned the contents of the folder, i dont know how to differentiate between the folders and the files to be able to delete the folders.
Also, i have been told that the rmdir() function cant delete folders which have content inside them, is there anyway around this?
Thanks, Ben.
-
First off, rmdir() cannot delete a folder with contents. If safe mode is disabled you can use the following.
exec("rm -rf folder/");
Also look at is_dir()/is_file() or even better the PHP SPL.
Ben McRae : your remove function worked a treat! what does rm -rf actually do? thanks -
To determine whether or not you have a folder or file use the functions is_dir() and is_file()
For example:
$path = 'extracted/' . $name[0]; $results = scandir($path); foreach ($results as $result) { if ($result === '.' or $result === '..') continue; if (is_dir($path . '/' . $result)) { //code to use if directory } }
Ben McRae : This is brilliant, works perfectly! Unfotunatly i dont understand what this part does 'if ($result === '.' or $result === '..') continue;', would you mind explaining please. Thanks again.: @Ben McRae: This is because scandir returns the results "." and ".." as part of the array, and in most cases you want to ignore those results, which is why I included that as part of the foreach -
$directories = scandir('images'); foreach($directories as $directory){ if($directory=='.' or $directory=='..' ){ echo 'dot'; }else{ echo $directory .'<br />'; } }
a simpler and perhaps faster version
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.