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.
<?
//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
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?
> 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).
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.
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
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...
<?
//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