Jump to content

delete folder and contents


dsjoes

Recommended Posts

You need to recursively delete the contents of the directory, before you can delete it. There's plenty of functions/snippets to search for through Google that should help you there. As vicodin suggested, you may run into permission issues as well.

i have tryed the below but i get 2 errors

<?php
$path = "../../gallery2.0/gallery_files/gallery/";
if(isset($_POST['file']) && is_array($_POST['file']))
{
foreach($_POST['file'] as $file)
{	
	$mydir = $path.$file; 
$d = dir($mydir); 
while($entry = $d->read()) { 
if ($entry!= "." && $entry!= "..") { 
unlink($entry); 
} 
} 
$d->close(); 
rmdir($mydir);
}

}
?>

 

these are the errors  the folder it is trying to delete is folder1 and the contents is just a picture called pic1.jpg but the errors say the picture isn't there but it is because it is picking its name up and the seconds says the folder is not empty.

 

Warning: unlink(pic1.jpg) [function.unlink]: No such file or directory in /hermes/bosweb/web230/b2302/ipg.removed/test_server/admin/gallery/index.php on line 63

 

Warning: rmdir(../../gallery2.0/gallery_files/gallery/Folder1) [function.rmdir]: Directory not empty in /hermes/bosweb/web230/b2302/ipg.removed/test_server/admin/gallery/index.php on line 67

You need to use a recursive function for this. Theoretically that sub-directory could contain a sub-directory, which contains a sub-directory, which contains a sub-directory, etc. You need to remove the contents of each before you can delete it. Rather than nesting loops within loops, just use a function that will repeatedly (recursively) call itself. I provided a Google link earlier that should help you.

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.