PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!
Moderator: General Moderators
crucialcomp
Forum Newbie
Posts: 6 Joined: Mon Apr 11, 2005 6:09 pm
Post
by crucialcomp » Tue Apr 12, 2005 12:57 pm
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
crucialcomp
Forum Newbie
Posts: 6 Joined: Mon Apr 11, 2005 6:09 pm
Post
by crucialcomp » Tue Apr 12, 2005 1:12 pm
BTW "12" is the directory name. In case anyone was wondering.
d3ad1ysp0rk
Forum Donator
Posts: 1661 Joined: Mon Oct 20, 2003 8:31 pm
Location: Maine, USA
Post
by d3ad1ysp0rk » Tue Apr 12, 2005 2:33 pm
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");
?>
crucialcomp
Forum Newbie
Posts: 6 Joined: Mon Apr 11, 2005 6:09 pm
Post
by crucialcomp » Tue Apr 12, 2005 4:20 pm
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
pickle
Briney Mod
Posts: 6445 Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:
Post
by pickle » Tue Apr 12, 2005 5:12 pm
if your using linux, just go:
Code: Select all
shell_exec('rm -rf /path/to/directory');
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
crucialcomp
Forum Newbie
Posts: 6 Joined: Mon Apr 11, 2005 6:09 pm
Post
by crucialcomp » Tue Apr 12, 2005 6:19 pm
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');
}
?>
crucialcomp
Forum Newbie
Posts: 6 Joined: Mon Apr 11, 2005 6:09 pm
Post
by crucialcomp » Tue Apr 12, 2005 6:20 pm
sorry one to many php tags in that last post.
pickle
Briney Mod
Posts: 6445 Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:
Post
by pickle » Tue Apr 12, 2005 9:01 pm
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.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
crucialcomp
Forum Newbie
Posts: 6 Joined: Mon Apr 11, 2005 6:09 pm
Post
by crucialcomp » Wed Apr 13, 2005 8:09 pm
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.
pickle
Briney Mod
Posts: 6445 Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:
Post
by pickle » Thu Apr 14, 2005 9:50 am
Best way to debug something like that is to fill it with echo statements...
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.