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";
}

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

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. 

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)

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

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.

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.