I don’t understand why the following code does not work. Can someone tell me what I did wrong?
The results of this code is shown below and indicates the file exists, but I can’t open the file.
This is similar to my previous post where I thought it was a permission problem when I was trying to write, but this is only a read request. Are files not read by default?
[CODE]
<?php
$filename = “..\Text\Text.txt”;
if (file_exists($filename)) {
echo “The file $filename exists”;
} else {
echo “The file $filename does not exist”;
}
$handle = fopen($filename, “r”)or die(“<br> can’t open file”);
fclose($handle);
?>
Here is the results of running this code,
[QUOTE]
The file ..TextText.txt exists
can’t open file
Check the file permissions. I'm not sure, but if you have read/execute on the directory, you may be able to "see" the file but unable to open it if you do not have read permission on the file itself -- and remember that in many installations your PHP script is run by the Apache user, not your login account user.[/QUOTE]
[code=php]
$filename = "../Text/Text.txt";
[/code]
[code=php]
<?php
//top of script:
ini_set('display_errors', 1); // set to 0 for production version
error_reporting(E_ALL);
[/code]
[CODE]
<?php
ini_set('display_errors', 1); // set to 0 for production version
error_reporting(E_ALL);
$filename = "temp.txt";
if (file_exists($filename)) {
echo "The file $filename exists";
} else {
echo "The file $filename does not exist";
}
$handle = fopen($filename, "w") or die("<br> can't open file");
fclose($handle);
?>
[/CODE]
Hmm...wish I knew what to say. I tried your script on my PC and did not get the die() message (setting $filename to a file in the same directory). ?[/QUOTE]
It really looks like a permissions problem. If I had to guess the user running the script (generally www-data, or apache) has read permission on the file but not write permission.[/QUOTE]
The php code indicates the file exists, but it will not either read or write form/to the file.[/QUOTE]
The code you've posted is trying to open the file for writing. If you switch the "w" to "r" does it still behave the same way? If so, perhaps the fopen function has had restrictions at the host level?[/QUOTE]
Write a small script that writes the file in the first place. Then it will have write permissions for the "owner" (creator) user.[/QUOTE]
Unfortunately chmod does not work on this server.[/QUOTE]
Write your PHP script to create the file initially...you shouldn't need chmod for files created by the php user.[/QUOTE]
[CODE]$fh=fopen($myFile,"w")or die("can't open file");[/CODE]
[code=php]
$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "Floppy Jalopyn";
fwrite($fh, $stringData);
$stringData = "Pointy Pinton";
fwrite($fh, $stringData);
fclose($fh);
[/code]
Try running something like this for a file that does not exist yet and make sure you are targeting a folder that has write permissions.
[code=php]
$myFile = "testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "Floppy Jalopyn";
fwrite($fh, $stringData);
$stringData = "Pointy Pinton";
fwrite($fh, $stringData);
fclose($fh);
[/code]
See if that will run for you.[/QUOTE]
First question...windows or linux hosting?
Second question...can you take a screen shot of your file browser showing the directory that has "write" permissions?
http://help.godaddy.com/article/2535#advanced
http://www.kntreed.com/post/2008/02/Changing-File-Permissions-with-Godaddy-Hosting.aspx
http://aackose.wordpress.com/2010/03/14/setting-write-permission-to-app_data-for-a-godaddy-hosted-site/ [/QUOTE]
ya, I was always leery of any website company who sells to the masses by paying crap tons to female models and Superbowl commercials, if they were smart they want a spokeswomen like Pauley Perrette, and then actually offer a service comparable to other host. Not to get off the subject but I would be baffled if it wasn't a permissions problem, that they seem not to care to support.[/QUOTE]
http://help.godaddy.com/article/2323 [/QUOTE]
[code=php]
$directoryWithPerm = "Your_Directory_You_Created_Here";
$myFile = $directoryWithPerm."/testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "Floppy Jalopyn";
fwrite($fh, $stringData);
$stringData = "Pointy Pinton";
fwrite($fh, $stringData);
fclose($fh);
[/code]
Did you try the code block from above?
[code=php]
[/QUOTE]
$directoryWithPerm = "Your_Directory_You_Created_Here";
$myFile = $directoryWithPerm."/testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "Floppy Jalopyn";
fwrite($fh, $stringData);
$stringData = "Pointy Pinton";
fwrite($fh, $stringData);
fclose($fh);
[/code]
Did you try the code block from above?
[code=php]
[/QUOTE]
$directoryWithPerm = "Your_Directory_You_Created_Here";
$myFile = $directoryWithPerm."/testFile.txt";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "Floppy Jalopyn";
fwrite($fh, $stringData);
$stringData = "Pointy Pinton";
fwrite($fh, $stringData);
fclose($fh);
[/code]
Have you unchecked the "inherit" checkbox? I saw some other people with a similar problem that said telling the directory not to inherit permissions from parent directories fixed the problem.[/QUOTE]
0.1.9 — BETA 5.20