fwrite () question. PLEASE HELP!!

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
User avatar
lazersam
Forum Contributor
Posts: 105
Joined: Sat Nov 15, 2003 4:07 am
Location: Hertfordshire, UK

fwrite () question. PLEASE HELP!!

Post by lazersam »

Hi all

I am trying to write to a text file as follows;

Code: Select all

<?php

	$filename = "test.txt";



	$result=fopen($filename, "a+");
	
	if (!$result){
	echo "Did not open file"; exit ();
	}
	
	fwrite ($filename, "Test\n\n");
	fclose($filename);
	
	
		

?>
It seems simple enough but i get this error....
Warning: fwrite(): supplied argument is not a valid stream resource in /home/lazersam/public_html/PHP/temp.php on line 16

Warning: fclose(): supplied argument is not a valid stream resource in /home/lazersam/public_html/PHP/temp.php on line 17
This is confusing because the fopen() statement works ok. I have set permissions for the directory but I am unable to set permission for test.txt which keeps defaulting back to 644.

Any ideas?

Thanks

Lawrence.
Last edited by lazersam on Sun Dec 07, 2003 6:58 am, edited 1 time in total.
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

Post by Pyrite »

Does the file get created though?
User avatar
lazersam
Forum Contributor
Posts: 105
Joined: Sat Nov 15, 2003 4:07 am
Location: Hertfordshire, UK

Post by lazersam »

Pyrite wrote:Does the file get created though?
Yes. It creates the file. :(

I think it might have something to do with my permissions on the server. When I try to chmod the file I get an error 550.
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

Post by Pyrite »

You can add a if (is_writable($filename)) { code to your script to first see if the file can even be written to before you call fopen. That way you know it will be a permissions issue between probably the user that your webserver runs under.

Are you using Linux? Is it your server? Do you have root? What happens if you create the file yourself, and then try to let your script write to it?
User avatar
lazersam
Forum Contributor
Posts: 105
Joined: Sat Nov 15, 2003 4:07 am
Location: Hertfordshire, UK

Post by lazersam »

Pyrite wrote:You can add a if (is_writable($filename)) { code to your script to first see if the file can even be written to before you call fopen. That way you know it will be a permissions issue between probably the user that your webserver runs under.

Are you using Linux? Is it your server? Do you have root? What happens if you create the file yourself, and then try to let your script write to it?
I ran the is_writable() statement and it proved the file IS writable. The script does open the file.

When I create the file myself it allows me to change its chmod permissions (?) however - it still give the same error. Do you know why it doesn't allow me to change permissions if PHP creates the file?
Warning: fwrite(): supplied argument is not a valid stream resource in..........
User avatar
Pyrite
Forum Regular
Posts: 769
Joined: Tue Sep 23, 2003 11:07 pm
Location: The Republic of Texas
Contact:

Post by Pyrite »

Cause the webserver/php run as a different user than you, and files that they create, you have different permissions to. Like Apache probably runs under nobody, or httpd or some user like that. I'm guessing its not your box though, so I don't know what to tell you. Can you ask your host about it?
User avatar
lazersam
Forum Contributor
Posts: 105
Joined: Sat Nov 15, 2003 4:07 am
Location: Hertfordshire, UK

Post by lazersam »

Thanks for trying Pyrite,

I have contacted my host but I hold out little hope :?

It seems so unlikely. I have searched the web for hours trying to fix this problem. All I want to do is write to a text file. Its chmod is 777, the directory chmod is 777 even! I think my syntax is all ok!

Can anyone else help?

I have even tried on a different domain but get the same results.

Thanks


Lawrence.
User avatar
lazersam
Forum Contributor
Posts: 105
Joined: Sat Nov 15, 2003 4:07 am
Location: Hertfordshire, UK

Post by lazersam »

Its OK I solved it.....

its was a simple case of wrong syntax :oops:

HOW IT WAS:

Code: Select all

<?php


   $filename = "test.txt"; 



   $result=fopen($filename, "a+"); 
    
   if (!$result){ 
   echo "Did not open file"; exit (); 
   } 
    
   fwrite ($filename, "Test\n\n"); 
   fclose($filename); 
    
    
       

?>
HOW IT SHOULD HAVE BEEN:

Code: Select all

<?php

   $filename = "test.txt"; 



   $result=fopen($filename, "a+"); 
    
   if (!$result){ 
   echo "Did not open file"; exit (); 
   } 
    
   fwrite ($result, "Test\n\n"); 
   fclose($result); 
    
    
       

?>
Thanks

Lawrence :P
Nay
Forum Regular
Posts: 951
Joined: Fri Jun 20, 2003 11:03 am
Location: Brisbane, Australia

Post by Nay »

You mean wrong variable <_<. Heh, anyhow, here's an alternative:

Code: Select all

<?php

$file = "test.txt";

$fd = fopen($file, "a+") or die("Did not open file: " . $filename);
$fw = fwrite("test", $fd);
fclose($fd);

?>
-Nay
User avatar
lazersam
Forum Contributor
Posts: 105
Joined: Sat Nov 15, 2003 4:07 am
Location: Hertfordshire, UK

Post by lazersam »

Nay wrote:You mean wrong variable <_<. Heh, anyhow, here's an alternative:

Code: Select all

<?php

$file = "test.txt";

$fd = fopen($file, "a+") or die("Did not open file: " . $filename);
$fw = fwrite("test", $fd);
fclose($fd);

?>
-Nay
Cool - thanks :wink:
User avatar
BDKR
DevNet Resident
Posts: 1207
Joined: Sat Jun 08, 2002 1:24 pm
Location: Florida
Contact:

Post by BDKR »

Nay wrote:You mean wrong variable <_<. Heh, anyhow, here's an alternative:

Code: Select all

<?php

$file = "test.txt";

$fd = fopen($file, "a+") or die("Did not open file: " . $filename);
$fw = fwrite("test", $fd);
fclose($fd);

?>
-Nay
I think the "or die()" stuff is bad practice. We should want our scripts to terminate in a more elegant manner shouldn't we? Testing for the error then doing something like displaying a more understandable error message to the end user would make them a lot happier.

Cheers,
BDKR
Post Reply