Odd behavior of fopen() in local directory

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
Elwoody
Forum Newbie
Posts: 3
Joined: Fri Nov 03, 2006 9:44 pm

Odd behavior of fopen() in local directory

Post 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
User avatar
feyd
Neighborhood Spidermoddy
Posts: 31559
Joined: Mon Mar 29, 2004 3:24 pm
Location: Bothell, Washington, USA

Post 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.
Elwoody
Forum Newbie
Posts: 3
Joined: Fri Nov 03, 2006 9:44 pm

Post 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
Last edited by Elwoody on Sat Nov 04, 2006 5:32 pm, edited 1 time in total.
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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).
Elwoody
Forum Newbie
Posts: 3
Joined: Fri Nov 03, 2006 9:44 pm

Post 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
timvw
DevNet Master
Posts: 4897
Joined: Mon Jan 19, 2004 11:11 pm
Location: Leuven, Belgium

Post 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.)
Post Reply