ballouta Posted June 21, 2008 Share Posted June 21, 2008 Hi I want to empty the directory temp, it is located here: /home/gscl/public_html/stk/temp How? Link to comment https://forums.phpfreaks.com/topic/111221-solved-how-to-empty-a-directory/ Share on other sites More sharing options...
Orio Posted June 21, 2008 Share Posted June 21, 2008 <?php function empty_dir($dir); //$dir should NOT be passed with an ending "/". { $result = true; $h = opendir($dir); if($h === false) return false; while($file = readdir($h)) { if($file == "." || $file == "..") continue; if(is_dir($file)) { if(!empty_dir($dir."/".$file)) $result = false; continue; } unlink($file); //Regular file.. } closedir($dir); return $result; } $dir = "/home/gscl/public_html/stk/temp"; if(!empty_dir($dir)) echo "An error occured. Some of the files weren't deleted."; else echo "Dir empty."; ?> Haven't tested it, use at your own risk. Orio. Link to comment https://forums.phpfreaks.com/topic/111221-solved-how-to-empty-a-directory/#findComment-570835 Share on other sites More sharing options...
ballouta Posted June 21, 2008 Author Share Posted June 21, 2008 Hi i changed this: $dir = "/home/gscl/public_html/stk/on"; on is an existing directory and contains 6 files I run the code but didn't delete anything: I got this: Warning: unlink(172129.ask) [function.unlink]: No such file or directory in /home/gscl/public_html/check2.php on line 212 Warning: unlink(357934004753637.ask) [function.unlink]: No such file or directory in /home/gscl/public_html/check2.php on line 212 Warning: unlink(172129nss.rpl) [function.unlink]: No such file or directory in /home/gscl/public_html/check2.php on line 212 Warning: unlink(172129.rpl) [function.unlink]: No such file or directory in /home/gscl/public_html/check2.php on line 212 Warning: unlink(357934004753637.rpl) [function.unlink]: No such file or directory in /home/gscl/public_html/check2.php on line 212 Warning: unlink(357934004753637nss.rpl) [function.unlink]: No such file or directory in /home/gscl/public_html/check2.php on line 212 Link to comment https://forums.phpfreaks.com/topic/111221-solved-how-to-empty-a-directory/#findComment-570841 Share on other sites More sharing options...
ballouta Posted June 21, 2008 Author Share Posted June 21, 2008 I found this one it is working properly thanks <?php function deleteDir($dir) { // open the directory $dhandle = opendir($dir); if ($dhandle) { // loop through it while (false !== ($fname = readdir($dhandle))) { // if the element is a directory, and // does not start with a '.' or '..' // we call deleteDir function recursively // passing this element as a parameter if (is_dir( "{$dir}/{$fname}" )) { if (($fname != '.') && ($fname != '..')) { echo "<u>Deleting Files in the Directory</u>: {$dir}/{$fname} <br />"; deleteDir("$dir/$fname"); } // the element is a file, so we delete it } else { echo "Deleting File: {$dir}/{$fname} <br />"; unlink("{$dir}/{$fname}"); } } closedir($dhandle); } // now directory is empty, so we can use // the rmdir() function to delete it echo "<u>Deleting Directory</u>: {$dir} <br />"; rmdir($dir); } // call deleteDir function and pass to it // as a parameter a directory name deleteDir("temporary"); ?> Link to comment https://forums.phpfreaks.com/topic/111221-solved-how-to-empty-a-directory/#findComment-570848 Share on other sites More sharing options...
Orio Posted June 21, 2008 Share Posted June 21, 2008 Found my problem, I think. Change this: unlink($file); Into: unlink($dir."/".$file); Orio. Link to comment https://forums.phpfreaks.com/topic/111221-solved-how-to-empty-a-directory/#findComment-570882 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.