Page 1 of 1

Odd behavior of fopen() in local directory

Posted: Fri Nov 03, 2006 9:56 pm
by Elwoody
Community: I am trying to write to a file with fopen. I can confirm it's existence.
Here be my snippet. No matter the path descriptor, it won't open. fopen() is enabled - confirmed via phpInfo. Error output below code.

Code: Select all

<?php
// $filename = "C:\\website\\slides\\05B.php";	      // this works for (a) and (b)
// $filename = "./05B.php";					// this works for (a) and (b)
// $filename = "../slides/05B.php";			    // this works for (a) and (b)

$filename = "../slides/05B.php";
chmod ($filename, 777);				// (a)
if (file_exists($filename)) {			  // (b)
		echo "<br>it's there<br>";
	} else {
		echo "<br>it  ain't there<br>";
	}
	$handle = fopen($filename, “r”);	// (c)  always fails
	fwrite($handle, 'Yo, sup?');                // (d)  always fails
	fclose($handle);                                // (e)  always fails
?>
Output:
it's there
Warning: fopen(../slides/05B.php) [function.fopen]: failed to open stream: No such file or directory in C:\website\slides\publisher_rhp.php on line 79
Warning: fwrite(): supplied argument is not a valid stream resource in C:\website\slides\publisher_rhp.php on line 80
Warning: fclose(): supplied argument is not a valid stream resource in C:\website\slides\publisher_rhp.php on line 81

I'm not terribly experienced with php, but I'm not at all new to app servers either...

thanks for your consideration,

Elwoody

Posted: Fri Nov 03, 2006 10:40 pm
by feyd
Try adding is_readable() and realpath(). They may return different results than you are expecting.

Chmod() expects octal values, just so you know.

Posted: Sat Nov 04, 2006 12:10 pm
by Elwoody
Thanks for the advice feyd - the information is useful, but doesn't seem to reveal the problem... any other suggestions?

Code: Select all

$filename = "../slides/05B.php";
	echo realpath($filename);
	$real_path = realpath($filename);
	$filename = $real_path;
	echo "<br>";
	if (is_readable($filename)) {
		echo 'The file is readable<br>';
	} else {
		echo 'The file is not readable<br>';
	}
	// chmod ($filename, 777);							// (a)
	if (file_exists($filename)) {							// (b)
		echo "<br>it's there<br>";
	} else {
		echo "<br>it  ain't there<br>";
	}
	$handle = fopen($filename, “w”);	 //tries to open a file for writing
	fwrite($handle, 'Yo, sup?');
	fclose($handle);
C:\website\slides\05B.php
The file is readable
it's there
Warning: fopen(C:\website\slides\05B.php) [function.fopen]: failed to open stream: No such file or directory in C:\website\slides\publisher_rhp.php on line 81
Warning: fwrite(): supplied argument is not a valid stream resource in C:\website\slides\publisher_rhp.php on line 82
Warning: fclose(): supplied argument is not a valid stream resource in C:\website\slides\publisher_rhp.php on line 83

Posted: Sat Nov 04, 2006 12:27 pm
by timvw
Elwoody wrote:

Code: Select all

$handle = fopen($filename, “r”);	 //tries to open a file for writing
Actually, that opens the file for reading... (And thus you can't write).

Posted: Sat Nov 04, 2006 5:36 pm
by Elwoody
timvw: Indeed - thanks for pointing that out. I've been switching all of the settings around trying to get some traction on a solution. I've corrected this in my code example above, but the script still fails to open, and thus read and close, the file - and yet the info functions report that everything should work. What else do you think the isssue might be? Might it be an issue with my apache installation? Php configuration -(aside from the fopen bit)?

Thanks,

Elwoody

Posted: Sat Nov 04, 2006 8:07 pm
by timvw
But you would need to know if your file [url=http:/www.php.net/is_writable]is_writable[/url] (And in order for this to be true, the user that is executing the php code, typically nobody or www-user would need to have permissions to do so.)