Page 1 of 1
Delete Folder and Contents? HELP!
Posted: Tue Apr 12, 2005 12:57 pm
by crucialcomp
I am trying to delete a folder and it's contents with a single script using a form action. Current code:
Code: Select all
<?php $dirnm = "12";
if (!isset($_POST['submit'])) {
?>
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<div align="center">
<input name="submit" type="submit" id="submit" value="submit">
</div>
</form>
<?php }
else {
function rmdirr($dirnm)
{
// Loop through the folder
$dir = dir($dirnm);
while (false !== $entry = $dir->read()) {
// Skip pointers
if ($entry == '.' || $entry == '..') {
echo $entry;
continue;
}
// Recurse
rmdirr("$dirnm/$entry");
}
// Clean up
$dir->close();
return rmdir($dirnm);
}
rmdirr();
}
?>
The code runs but does not do anything as far as deleting the folder or any of its contents. Any suggestions are appreciated. Thanks ahead of time.
Jcart | Please use
Posted: Tue Apr 12, 2005 1:12 pm
by crucialcomp
BTW "12" is the directory name. In case anyone was wondering.
Posted: Tue Apr 12, 2005 2:33 pm
by d3ad1ysp0rk
I found this very useful function on php.net that does exactly what you're explaining.
Code: Select all
<?php
class deleter(){
function delDir($dirName) {
if(empty($dirName)) {
return true;
}
if(file_exists($dirName)) {
$dir = dir($dirName);
while($file = $dir->read()) {
if($file != '.' && $file != '..') {
if(is_dir($dirName.'/'.$file)) {
$this->delDir($dirName.'/'.$file);
} else {
@unlink($dirName.'/'.$file) or die('File '.$dirName.'/'.$file.' couldn\'t be deleted!');
}
}
}
$dir->close();
@rmdir($dirName) or die('Folder '.$dirName.' couldn\'t be deleted!');
} else {
return false;
}
return true;
}
}
$obj = new deleter;
$obj->delDir("12");
?>
Posted: Tue Apr 12, 2005 4:20 pm
by crucialcomp
using that kicks out an error:
Parse error: parse error, unexpected '(', expecting '{' in /vservers/patsfloralde/htdocs/purgeimages.php on line 73
Any chance you can post the link to the original script on php.net as i cannot find it. Thanks
Posted: Tue Apr 12, 2005 4:43 pm
by John Cartwright
Moved to PHP-Code
Posted: Tue Apr 12, 2005 5:12 pm
by pickle
if your using linux, just go:
Code: Select all
shell_exec('rm -rf /path/to/directory');
Posted: Tue Apr 12, 2005 6:19 pm
by crucialcomp
the server is an apache server (hosted through smarterlinux.com). the script line:
Code: Select all
shell_exec('rm -rf /path/to/directory');
Does not kick any error message but also does not delete the folder. I am not sure if this has to do with there being files in the folder or not.
Current page I am working with:
Code: Select all
<?php if (!isset($_POST['submit'])) {
?>
Purge Images Folder
<form action="<?php echo $_SERVER['PHP_SELF']; ?>" method="post">
<div align="center">
<input name="submit" type="submit" id="submit" value="submit">
</div>
</form>
<?php }
else {
shell_exec('rm -rf /12');
}
?>
Posted: Tue Apr 12, 2005 6:20 pm
by crucialcomp
sorry one to many php tags in that last post.
Posted: Tue Apr 12, 2005 9:01 pm
by pickle
shell_exec stores the output of the command in a string, so:
Code: Select all
$return_value = shell_exec('rm -rf /12');
echo $return_value;
should give you a message. Regardless of which way you decide to do this, you should still check to see if apache has rights to remove those directories, as that's the user PHP will be acting as.
Posted: Wed Apr 13, 2005 8:09 pm
by crucialcomp
Does not display any info on submit so i am beginning to wonder if the form is submitting properly or if the php is handling the submission properly. Still cracking away at it. Advise is appreciated. Thanks pickle.
Posted: Thu Apr 14, 2005 9:50 am
by pickle
Best way to debug something like that is to fill it with echo statements...