-
How do I unlink this?
I have a very simple question, I guess.
I need to remove a folder and all it's contents. Not sure if I need to change the permissions as well.
This is what I have right now, that isn't working.
PHP Code:
$file = "../images/".$code;
unlink($file);
The name of the folder that is going to be deleted is in $code.
Should this code work? Do I only need to change the chmod?
If so, how can I do this prior to the delete code?
-
-
Thank you. That one worked for me with an empty folder.
But since I have things in the folder as well I tried to add something, but it won't delete my files. Why not?
PHP Code:
unlink("$file/*.*");
rmdir($file); # deletes the folder
Then I tried to add one code that deletes all file types I think will be in the folder with this code.
But that one isn't working either.
PHP Code:
unlink("$file/*.mp3");
unlink("$file/*.wav");
unlink("$file/*.aif");
unlink("$file/*.jpg");
unlink("$file/*.png");
unlink("$file/*.gif");
rmdir($file); # deletes the folder
What is wrong with my code?
When using just the rmdir to remove an epmty folder it works fine, but I have to remove things in the folder first...
-
I solved it finally.
I found something that worked.
The one called $file is for the folder. Then I made one called $file2 for the filenames inside the folder.
PHP Code:
$file = "../images/".$code;
$file2 = $file.'/*';
foreach (glob("$file2.*") as $filename) {
unlink($filename);
}
rmdir($file); # deletes the folder