Jump to content

[SOLVED] Empty directory + delete directory afterwards


EchoFool

Recommended Posts

I have a script to delete a directory plus all its contents inside the directory before hand... but i get an error every time i try it.....

 

 

This is my code:

 

<?php
$ID = strtolower(mysql_real_escape_string(stripslashes($_POST['ID'])));
$mydir = "images/$ID/"; 
$d = dir($mydir); 
while($entry = $d->read()) { 
if ($entry!= "." && $entry!= "..") { 
unlink($entry); 
} 
} 
$d->close(); 
rmdir($mydir); 
?>

 

But i keep getting this response :

 

Warning: unlink(1245096028.jpg) [function.unlink]: No such file or directory in listings.php on line 72

Warning: unlink(1245096033.jpg) [function.unlink]: No such file or directory in listings.php on line 72

Warning: unlink(1245096037.jpg) [function.unlink]: No such file or directory in listings.php on line 72

Warning: unlink(1245096041.jpg) [function.unlink]: No such file or directory in listings.php on line 72

Warning: unlink(1245096045.jpg) [function.unlink]: No such file or directory in listings.php on line 72

Warning: unlink(1245096050.jpg) [function.unlink]: No such file or directory in listings.php on line 72

Warning: unlink(1245096054.jpg) [function.unlink]: No such file or directory in listings.php on line 72

Warning: unlink(1245096062.jpg) [function.unlink]: No such file or directory in listings.php on line 72

Warning: unlink(1245096065.jpg) [function.unlink]: No such file or directory in listings.php on line 72

Warning: rmdir(images/13/) [function.rmdir]: Directory not empty in listings.php on line 76

 

My directory is correct otherwise i wouldn't get the final error of:

 

Warning: rmdir(images/13/) [function.rmdir]: Directory not empty in listings.php on line 76

 

What have i got wrong?

 

Hope you can help.

You can always just try this.

 

 

$ID = strtolower(mysql_real_escape_string(stripslashes($_POST['ID'])));
$mydir = "images/$ID/";
system("rm ${mydir} -rf");

 

but to answer your question, you need to add the directory in with the unlink.

 

$ID = strtolower(mysql_real_escape_string(stripslashes($_POST['ID'])));
$mydir = "images/${ID}/"; 
$d = dir($mydir); 
while($entry = $d->read()) { 
if ($entry!= "." && $entry!= "..") { 
	unlink($mydir.$entry); 
}
}
$d->close(); 
rmdir($mydir);

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.