PDA

View Full Version : Fwrite doesn't work !!!



saleh
06-23-2007, 07:28 PM
Hello, my problem is :
fwrite() and fclose() php functions don't work in my account but fopen works
My script is :


$file = "/home/my account/public_html/file.txt";
fopen ($file, 'a+');
if (file_exists($file))
echo "File exists<br /><br />";
else
echo "File doesn't exists<br /><br />";

if (is_writable($file))
echo "File is writable<br /><br />";
else
echo "File is not writable<br /><br />";

$string = "text text ";
fwrite($file, $string);
fclose ($file);


My executed page looks like :


File exists

File is writable


Warning: fwrite(): supplied argument is not a valid stream resource in /home/My account/public_html/generator.php on line 15

Warning: fclose(): supplied argument is not a valid stream resource in /home/My account/public_html/generator.php on line 16


So, what's the problem guys ? any misconfiguration in php.ini, or a special configuration for php5 ?

pt7zz
06-23-2007, 10:58 PM
Same Here .....:confused:

And i need to run a php script, which working in my old server and don't run at Hostmonster ... :(

By the way, php.ini in all folders allready.



File exists

File is writable


Warning: fwrite(): supplied argument is not a valid stream resource in /home/my_account/public_html/test.php on line 16

Warning: fclose(): supplied argument is not a valid stream resource in /home/my_account/public_html/test.php on line 17

charlesgan
06-24-2007, 01:17 AM
you open the stream for APPEND only.
try to use 'w+' .. for writing

saleh
06-24-2007, 07:25 AM
you open the stream for APPEND only.
try to use 'w+' .. for writing

Hello, Thank you for responding ...
I need to append actually in my urllist.txt fot my urls
and the w+ deletes all content of my txt file before writing ...

I think that's not the problem, why 'a+' doesn't work ?
It works everywhere ...

sjlplat
06-24-2007, 01:44 PM
Append is fine. If the file is nonexistent or empty, append will simply create the file, or add the text.

However, I don't see any problem with the code. The only thing I can think of is it has something to do with PHP5. I don't know how PHP5 differs from PHP4, so I can't be a lot of help. :(

r2b2
06-24-2007, 04:50 PM
Suggest maybe contacting the HostMonster support guys - maybe its disabled for some (strange) reason...

saleh
06-24-2007, 06:57 PM
Hi, that's me again and I'm here with the solution, it was not a server misconfiguration but the code, yes that's right, it appears that my basic code doesn't work with all versions of PHP, as my hoster @ Monster runs php5, the exact code was :


$file = 'test.txt';
$content = "Some contents\n";
if (is_writable($file))
{
$handle = fopen($file, 'a');
fwrite($handle, $content);
fclose($handle);
}

and the text is appended in the text file.
Note that the $handle variable is important ...
That's it
Bye ...

sjlplat
06-24-2007, 06:59 PM
I don't know why I didn't think of that. I write all my code that way. :confused:

Set the fopen() function as a variable, then call it with fwrite(). I don't know that I've ever tried it the other way, so it really didn't stand out.

mdocea
06-25-2007, 07:18 PM
fwrite() and fclose() work on both php4 and php5. Unfortunately it looks like you might be having problems with your code.

In order to demonstrate that fwrite() and fclose() are working on the server I suggest creating two files:

public_html/test/fwrite.php
&
public_html/test/test.txt

You can find the code that you should use for fwrite.php here: http://www.php.net/manual/en/function.fwrite.php

The test.txt is just a blank file.

If you browse to: http://www.yourdomain.com/test/fwrite.php you will see that the script works and fwrite() & fclose() both work.