Jump to content

[SOLVED] remove folder from server


kev wood

Recommended Posts

I believe this will do the trick. Just be careful when your playing with it.

 

// Path to directory you want to delete
$directory = 'dir';

// Delete it
if (rmdir($directory))
{
    echo "{$directory} has been deleted";
}
else
{
    echo "{$directory} could not be deleted";
}

Link to comment
Share on other sites

I just tried the above code and even with a folder CHMOD of 777, it doesn't work, so I'll let someone else pick up the ball on this one. I'm pretty sure it involves using rmdir though.

 

Warning: rmdir(/home/user/public_html/test/asd/) [function.rmdir]: Permission denied in /home/user/public_html/test/test.php on line 7

/home/user/public_html/test/asd/ could not be deleted

Link to comment
Share on other sites

dont no why that didnt work on a empty folder according to the php manual it only works on empty folders and the unlink must be used first to empty the folder.

 

i was looking at the unlink function earlier but some of the files have had random numbers appended to them so to get the unlink file to work i need to know the full file name first to get it to work i think.  i may be wrong tho i will go check that out now. 

Link to comment
Share on other sites

Indeed. You can only delete a directory once it is empty; you will have to remove all the files first.

 

If there are no subdirectories in the folder you wish to delete, you could use:

 

<?php
$dir = 'test2/';
foreach(glob($dir.'*.*') as $v){
    unlink($v);
}
rmdir($dir);
?>

 

Otherwise, you'll have to write a recursive function (examples of which can be found on the user contributed notes in the manual here or here)

Link to comment
Share on other sites

you could try:

 

exec('rm -fr ' . $dir);

 

this is a basic shell cmd so if your install has shell access and permissions to the dir, your set...otherwise, your sol

 

rm = remove

-fr are flags for the cmd. f = force, r = recursive

 

hope this helps.

Link to comment
Share on other sites

gingerobot

 

am i right in thinking that this will delete all files from within the directory and then remove the directory.  from what i have gathered it looks for files with a . in them and removes these.

 

mlin

 

thanks for your reply but i know a little bit about the reply before yours so i will look at going down this road for now

Link to comment
Share on other sites

cool, I like to stick with what I know as well. Ginger's solution will work for sure as long as there are no subdir's in the folder as she said. Add recursion to her solution and subdir's won't matter either

Link to comment
Share on other sites

i changed the code ever so slightly to work exactly how i wanted.  i need it for an image upload folder.  when an image is uploaded a thumb nail is created and the original image are both stored on the server in there respective folders.  i need to give the user the option to remove all the file that had been uploaded and this code does exactly what i needed it to.

 

thank you to all who replied.  here is the final code i used

 

$dir = 'image/thumbs/';
foreach(glob($dir.'*.*') as $v){
    unlink($v);
}

$dir2 = 'image/';
foreach(glob($dir2.'*.*') as $v){
    unlink($v);
}

 

the first part deletes all the thumbnail images and the second all the original images.

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.