GetDotted Domains

Viewing Thread:
"PHP - can't write a new text file in htdocs?"

The "Freeola Customer Forum" forum, which includes Retro Game Reviews, has been archived and is now read-only. You cannot post here or create a new thread or review on this forum.

Fri 30/06/06 at 17:08
Regular
Posts: 46
Hi, I hope someone can help me. I need to create a new text file from a php script. At the moment I'm testing things out (as I'm new to Freeola). I've written this simple code:


<?
//write a small file
$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die(":\nCan't open file");

$stringData = "line 1\n";
fwrite($fh, $stringData);
$stringData = "line 2\n";
fwrite($fh, $stringData);
fclose($fh);
?>


But when I run it I get an error message:
"Warning: fopen(testFile.txt): failed to open stream: Permission denied in /freeola/users/6/6/sr0405566/htdocs/testing.php on line 4
can't open file"

Am I being stupid here? Can anyone help?

Jo
Sun 02/07/06 at 14:09
Regular
Posts: 46
Hi, thanks Garin.

If anyone else has this problem at any time here's a summary of what I did to allow me to create a new file from a php script:

1. Created a new directory inside htdocs, we'll call it 'newDir'.
2. Made sure the permissions on newDir are rwxr-xr-x (by typing 'ls').
(3. If they're not, use the chmod command to change them - 'chmod 755 newDir'.)
4. In my php script I declared my new file as '$myFile = "./newDir/testFile.txt";'.

Then all was hunky-dory.

Jo

PS - does anyone know why the string method for chmod (e.g. chmod u+rwx) doesn't work?
Sat 01/07/06 at 15:47
Regular
"Devil in disguise"
Posts: 3,151
Dr Jo wrote:
> I've checked the permissions for htdocs, and as I'd expect
> they're rwxr-xr-x, which means only the user has write ability
> for the directory. So I don't want to change that. So I still
> don't understand why I can't create a new file from within a
> script.

Apache/PHP will be running as a different user to the one that owns your htdocs directory. The user that has the ability to write in that directory is the one created for your FTP account, not the apache/php process.
Create a directory in htdocs, and make sure everyone can read/write to it (you dont want to be changing the permissions of htdocs anyway).
Sat 01/07/06 at 10:45
Regular
"Picking a winner!"
Posts: 8,502
I think you need to change the permissions to allow anyone to write to the folder. Worth trying.

If you have other files in the folder and you don't want people writing to that folder you could create a new one for the script and use that instead.
Sat 01/07/06 at 09:01
Regular
Posts: 46
Thanks for that.

I am creating a new file rather than writing to an existing one, so I think the permissions I need to check are those for the directory I'm trying to create the new file in - htdocs - if that makes sense.

I've checked the permissions for htdocs, and as I'd expect they're rwxr-xr-x, which means only the user has write ability for the directory. So I don't want to change that. So I still don't understand why I can't create a new file from within a script.

Any guesses?

Jo
Fri 30/06/06 at 17:33
Moderator
"Are you sure?"
Posts: 5,000
Hi Dr. Jo, welcome to Freeola!

I don't do PHP, but seeing the 'permissions' error, perhaps you need to CHMOD (change permissions) the file you are writing?

I'm sure some of the PHP guro's around here will be able to help you more...

EDIT: I've just 'googled' your error and it does look like a CHMOD problem. You'll need to change the file permissions (using an FTP application). If you need help with CHMOD please shout...
Fri 30/06/06 at 17:08
Regular
Posts: 46
Hi, I hope someone can help me. I need to create a new text file from a php script. At the moment I'm testing things out (as I'm new to Freeola). I've written this simple code:


<?
//write a small file
$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die(":\nCan't open file");

$stringData = "line 1\n";
fwrite($fh, $stringData);
$stringData = "line 2\n";
fwrite($fh, $stringData);
fclose($fh);
?>


But when I run it I get an error message:
"Warning: fopen(testFile.txt): failed to open stream: Permission denied in /freeola/users/6/6/sr0405566/htdocs/testing.php on line 4
can't open file"

Am I being stupid here? Can anyone help?

Jo

Freeola & GetDotted are rated 5 Stars

Check out some of our customer reviews below:

Great services and friendly support
I have been a subscriber to your service for more than 9 yrs. I have got at least 12 other people to sign up to Freeola. This is due to the great services offered and the responsive friendly support.
LOVE it....
You have made it so easy to build & host a website!!!
Gemma

View More Reviews

Need some help? Give us a call on 01376 55 60 60

Go to Support Centre
Feedback Close Feedback

It appears you are using an old browser, as such, some parts of the Freeola and Getdotted site will not work as intended. Using the latest version of your browser, or another browser such as Google Chrome, Mozilla Firefox, or Opera will provide a better, safer browsing experience for you.