Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PHP - rmdir (permission denied)

I have an easy script to create and delete a folder, but when I try to delete a folder, it brings up and error.

The code:

<?php
if ($_POST['hidden']) {
$key = "../g_test/uploads";
$new_folder = $_POST['nazevS'];
$new_dir_path = $key."/".$new_folder;
$dir = mkdir($new_dir_path);    
if($dir)
chmod ($new_dir_path, 0777); 
}
if ($_POST['hiddenSS']) {
$key = "../g_test/uploads";
$new_folder = $_POST['nazevS'];
rmdir($key."/".$new_folder);
}
?>

The error msg:

Warning: rmdir(../g_test/uploads/) [function.rmdir]: Permission denied in /home/free/howto.cz/m/mousemys/root/www/g_test/upload.php on line 51

Does anyone know how to delete the folder (hopefuly with everything inside) ? Also if you see any other improvments, the code could have, feel free to tell me. :-)

Thanks, Mike.

like image 730
Mike Avatar asked Dec 30 '22 18:12

Mike


1 Answers

Generally speaking PHP scripts on Unix/Linux run as user "nobody", meaning they need the "all" privileges so it's a permissions problem with the directory. Also, to delete a file or directory in Linux/Unix you need write privileges on the parent directory. That might be your problem.

If you have problems with the files or directories you create, use chmod() on them to set the right permissions.

Also it might not be empty.

Also, it's worth mentioning that

$new_folder = $_POST['nazevS'];
$new_dir_path = $key."/".$new_folder;

is really bad from a security point of view. Sanitize that input.

like image 166
cletus Avatar answered Jan 09 '23 02:01

cletus