Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations gkittelson on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

PHP fwrite not writing on linux box

Status
Not open for further replies.

rninja

Technical User
Apr 11, 2001
381
US
I am still having problems getting php to work. I removed all php program files from the linux server, then recompiled and installed php again.

I can append to a file using fwrite but cannot use the option "w" to write to a file. I am testing it using a very simple write:


<?
$line = "Written!";
// Open the file and erase the contents if any
$fp = fopen("test.inc", "w") or die("Couldn't open the file");
//Write the data to the file
fwrite($fp, $line) or die("Couldn't write to file");
// Close the file
fclose($fp);
echo "Done.";
?>


Can anyone advise?

Rninja

sl_sm.jpg

 
check the permissions. does your [apache] user have permission to create files as well as read/write?
 
As I mentioned, I can append ("a") without a hitch, permissions are set properly, I even experimented by leaving the file writable by nobody and set to 777. No go.

Any other ideas?

Rninja

sl_sm.jpg

 
Can you log onto the unix box in the user php runs under and do what you need to do?
Where does the error come, on the open or the write ?
 
not much more to suggest, I'm afraid: a few thoughts though:

+ try adding error_reporting(E_ALL) before the code to make sure you are capturing all warnings.

+ if you use fopen("filename","a") and a file does not already exist, does it work?

+ is safe more turned on? if so, try turning it off and rerunning your code.
 
+ try adding error_reporting(E_ALL) before the code to make sure you are capturing all warnings.

>> I have this enabled and get no errors on most pages and the ones that do are incorrect. For instance here is the code:

<?
error_reporting(E_ALL);
$line="This is a line";
// Open the file and erase the contents if any
$fp=fopen("test.inc", "w") or die();
//Write the data to the file
fwrite($fp, $line) or die();
// Close the file
fclose($fp);
echo "Done.";
?>

and I get this error -
<code>Parse error: parse error, unexpected '\"' in /httpd/test.php on line 5</code>

+ if you use fopen("filename","a") and a file does not already exist, does it work?

>> If I use fopen("filename", "a") and there is no file, it does nothing.
If I use fopen("filename", "a") and there is a file, it does append.
If I use fopen("filename", "w") and there is a file, it erases and doesn't enter any data.
If I use fopen("filename", "w") and there is no file, it creates it and nothing else. Permissions are set to 644 and user/group nobody.

Even if I chmod the file that it writes to and directory to 777, it does not write into the file, but will create it if it is not around. So it works to append data to an already existant file, reads from the file, but does not write.

+ is safe more turned on? if so, try turning it off and rerunning your code.

>> I tried with safemode on and off, same result. I have safemode off.

Additionally, If I try running the file (in this case: test.php) through the php interpreter by command line like so:
<code>[user@localhost user]# php test.php</code>
it prints: "done." and the file is written into.

I've also tried removing all php program files from the server using both php-4.3.10 and php-5.0.3 with nothing changing, the same scenario.

Rninja

sl_sm.jpg

 
+ try adding error_reporting(E_ALL) before the code to make sure you are capturing all warnings.

>> I have this enabled and get no errors on most pages and the ones that do are incorrect. For instance here is the code:

<?
error_reporting(E_ALL);
$line="This is a line";
// Open the file and erase the contents if any
$fp=fopen("test.inc", "w") or die();
//Write the data to the file
fwrite($fp, $line) or die();
// Close the file
fclose($fp);
echo "Done.";
?>

and I get this error -
Code:
Parse error: parse error, unexpected '\"' in /httpd/test.php on line 5

+ if you use fopen("filename","a") and a file does not already exist, does it work?

>> If I use fopen("filename", "a") and there is no file, it does nothing.
If I use fopen("filename", "a") and there is a file, it does append.
If I use fopen("filename", "w") and there is a file, it erases and doesn't enter any data.
If I use fopen("filename", "w") and there is no file, it creates it and nothing else. Permissions are set to 644 and user/group nobody.

Even if I chmod the file that it writes to and directory to 777, it does not write into the file, but will create it if it is not around. So it works to append data to an already existant file, reads from the file, but does not write.

+ is safe more turned on? if so, try turning it off and rerunning your code.

>> I tried with safemode on and off, same result. I have safemode off.

Additionally, If I try running the file (in this case: test.php) through the php interpreter by command line like so:
Code:
[user@localhost user]# php test.php
it prints: "done." and the file is written into.

I've also tried removing all php program files from the server using both php-4.3.10 and php-5.0.3 with nothing changing, the same scenario.

Rninja

sl_sm.jpg

 
i assume that the line 5 referred to in the error message is the fopen clause? in which case i suspect that that the fopen clause you are using to test the script is not identical to that which is being posted here. if so, could you post the actual fopen clause you are using? if i am wrong, could you tell me which line is line 5 in your code?
 
This is the identical script, I realized that it had a trailing " and not ;, I changed it in this script and the error is no longer. Problem is, despite the fix, same problem exists.

Rninja

sl_sm.jpg

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top