Web Hosting Talk







View Full Version : I get permission denied when trying to create a directory with PHP


lordg
01-25-2007, 12:31 AM
Here's my code

mkdir($_SERVER["DOCUMENT_ROOT"].'/folder');

When I try to run that, I get this error

Warning: mkdir(/home/zeefvdor/public_html/folder): Permission denied in /home/zeefvdor/public_html/test.php on line 51


This code worked perfectly on the old server I was on, but now on the new server, it does not work?

Anyone have any ideas?

ub3r
01-25-2007, 12:53 AM
Your old server was probably running phpsuexec, and your new server is running mod_php.

Try and chmod your parent directory to 777.

lordg
01-25-2007, 02:20 AM
Ok that seems to work.

Now i have a new problem. When I create a file called "text.txt" with PHP using the fopen function, I can't seem to delete the file when I'm using the FTP program.

However when I use a PHP script using the function unlink, it deletes the file sucessfully.


Why can't I delete the file with the FTP program?

StevenG
01-25-2007, 04:33 AM
Why can't I delete the file with the FTP program?

Because mod_php is probably running as 'nobody' or 'www-data' (default apache user. Any files created using php will therefore be owned by the user that created them, in your case, the apache 'user'.

lordg
01-25-2007, 03:16 PM
Because mod_php is probably running as 'nobody' or 'www-data' (default apache user. Any files created using php will therefore be owned by the user that created them, in your case, the apache 'user'.


So there's no way for me to delete the file when it's being created by PHP, other than using PHP itself to remove it?

ThatScriptGuy
01-25-2007, 04:13 PM
You could delete it using SSH or from the cpanel file manager.
Kevin

insanelymacintosh
01-25-2007, 05:59 PM
Perhaps it creates the file with the wrong permissions? You can try

<?php
// code to create the file
bla bla bla

// change file permissions
chmod($filename,0777);
?>

on the page you use when creating the file.

horizon
01-25-2007, 07:10 PM
<?php

if (is_writable($_SERVER["DOCUMENT_ROOT"])."/folder") {
mkdir($_SERVER["DOCUMENT_ROOT"]."/folder/sub_folder");
}

?>

I'd not advise to create dirs from the DOCUMENT_ROOT folder. I'd rather suggest create a new writable folder. Then, create a new dir from inside that writable folder for security reasons.

HIU-Daniel
01-25-2007, 09:58 PM
Ok that seems to work.

Now i have a new problem. When I create a file called &quot;text.txt&quot; with PHP using the fopen function, I can't seem to delete the file when I'm using the FTP program.

However when I use a PHP script using the function unlink, it deletes the file sucessfully.


Why can't I delete the file with the FTP program? You could try using the "chown" command to change the owner of the file after you create it.

dkitchen
01-25-2007, 10:13 PM
You could try using the "chown" command to change the owner of the file after you create it.

If the user doesn't have access to the file they won't be able to chown it...

Dan

horizon
01-25-2007, 10:27 PM
The chown command is only accessable by the root account or if the root account assigns permissions towards users. From there, users can modify permissions over their files.