Jump to content

delete folder and contents


dsjoes

Recommended Posts

how do i make this part of the script delete the folder and contents.

 

<?php
$path = "../../gallery/gallery_files/gallery/";
if(isset($_POST['file']) && is_array($_POST['file']))
{
foreach($_POST['file'] as $file)
{	
	rmdir($path. "/" . $file) or die("Failed to delete file");
}

}
?>

Link to comment
Share on other sites

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

Link to comment
Share on other sites

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.

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.