PHP & Writing to Files

Whether you are using Linux on the desktop or as a server, it's still good that you're using Linux. Linux related questions go here.

Moderator: General Moderators

Post Reply
User avatar
ashonline
Forum Newbie
Posts: 4
Joined: Sat Nov 05, 2005 5:15 pm

PHP & Writing to Files

Post by ashonline »

Hi

I have an issue on my Fedora Core 4 installation where PHP scripts can't write to text files.

As an example, I get the following in Apache error logs:
PHP Warning: fwrite(): supplied argument is not a valid stream resource...
PHP Warning: fclose(): supplied argument is not a valid stream resource...

Apache is running as user "apache", with the files in question having been chown'd to apache & chmod'd to 777.
SELinux is disabled.

"Safe mode" in php.ini is set to off, & it's configuration hasn't been altered since installation.

Running Apache 2.0.54-10.2 & PHP 5.0.4-10.4.

Can someone please help me with this one?

Thanks

Ashley
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

Post code
User avatar
ashonline
Forum Newbie
Posts: 4
Joined: Sat Nov 05, 2005 5:15 pm

Post by ashonline »

The code is from a tutorial (i'm a php newbie)...

Code: Select all

$fp = fopen("$DOCUMENT_ROOT/../php_files/orders.txt", 'ab');
fwrite($fp, $outputstring, strlen($outputstring));
fclose($fp);
I've had similar issues with writing to files when using phpBB & Subdreamer also.
User avatar
John Cartwright
Site Admin
Posts: 11470
Joined: Tue Dec 23, 2003 2:10 am
Location: Toronto
Contact:

Post by John Cartwright »

change

Code: Select all

$DOCUMENT_ROOT
to

Code: Select all

$_SERVER['DOCUMENT_ROOT']
User avatar
ashonline
Forum Newbie
Posts: 4
Joined: Sat Nov 05, 2005 5:15 pm

Post by ashonline »

Sorry, I should have mentioned that at the top of the file I have:

Code: Select all

$DOCUMENT_ROOT = $_SERVER['DOCUMENT_ROOT'];
User avatar
pickle
Briney Mod
Posts: 6445
Joined: Mon Jan 19, 2004 6:11 pm
Location: 53.01N x 112.48W
Contact:

Post by pickle »

put $DOCUMENT_ROOT/../php_files/orders.txt in a variable, and echo that variable. It may be different than you expect.
Real programmers don't comment their code. If it was hard to write, it should be hard to understand.
Post Reply