PDA

View Full Version : [PHP] Using touch() to create a file



jw06
February 13th, 2005, 04:16 PM
I'm trying to create a file when I add a new entry to my MySQL Database, but I keep getting this error:

Warning: unable to create file /home/portfolio/www/portfolio/102984.php because Permission denied in /home/portfolio/www/admin/admin_portfolio.php on line 49

I have chown'd both directories (admin & portfolio), but it still isnt allowing me to create the file. Any idea on what I can do?

Code:


$filename = $_POST['link'];
if (!file_exists($_SERVER['DOCUMENT_ROOT'].'/portfolio/' . $filename)) {
touch ($_SERVER['DOCUMENT_ROOT'].'/portfolio/' . $filename);
}

Hans Kilian
February 13th, 2005, 05:31 PM
Your PHP script is running under the webservers user-ID, so that's the one that needs to be able to create the file.

You can find out what user-ID that is by running this script:


<?php
echo `whoami`;
?>

(Note the 'weird' quotes around whoami)
The script assumes that your server is running on some sort of Unix/Linux operating system.

RushScripting
February 13th, 2005, 05:32 PM
ummm touch() isn't used to create files, it is used for access times and such. I think fwrite() or something of the such should do the job.

Hans Kilian
February 13th, 2005, 05:44 PM
Ummm, from the manual: "If the file does not exist, it is created".

jw06
February 13th, 2005, 06:16 PM
Whoami returns: www-data

This is the group apache uses on my server.

any idea?

Hans Kilian
February 14th, 2005, 12:44 AM
Then you'll have to use chmod and/or chown to set the access up so the user www-data is able to create files in your directories.

chmod 777 <directory>

ought to do it. But that may be a bit risky if you share the machine with other users, since everybody will be able to do anything in the directories.