Forum Moderators: coopster

Message Too Old, No Replies

fwrite() problem

supplied argument is not a valid stream resource

         

mhbweb

12:36 pm on Sep 22, 2004 (gmt 0)

10+ Year Member



i am new to PHP so please excuse this if its a stupid question.

i am opening a text file for appending, the file opens no problem but when i try to write data to it I get this error message from the server;

supplied argument is not a valid stream resource

what am I doing wrong?

ps. permissions are all set up 777 on all files used.

kumarsena

1:06 pm on Sep 22, 2004 (gmt 0)

10+ Year Member



can u post the that u use?

kumarsena

1:06 pm on Sep 22, 2004 (gmt 0)

10+ Year Member



sorry...the code i mean..he he

mhbweb

1:14 pm on Sep 22, 2004 (gmt 0)

10+ Year Member



if (is_writable($filename)) {
if ($file=fopen($filename, "ab") == FALSE) {
echo "The file could not be opened";
} else {
if (fwrite($file, $name) === TRUE) {
echo "file writen successfully";
fclose($file);
} else {
echo "file not written";}
} else {
echo "the file is not writable";}

thx for the help, its driving me nuts

variables are obviously set previously

kumarsena

1:58 pm on Sep 22, 2004 (gmt 0)

10+ Year Member



hey, have sorted your problem....

before i explain, here is the new code (with added vars i used):

<?php

$filename = 'test.txt';
$name = 'kumar';

if (is_writable($filename))
{
if (!$file=fopen($filename, "ab") )
{
echo "The file could not be opened";
}

else
{
if (fwrite($file, $name))
{
echo "file writen successfully";
fclose($file);
}

else
{
echo "file not written";
}
}

}

else
{
echo "the file is not writable";
}

?>

now there are few things that i found.

1. unclosed else loop..else opened in line 4 in the code u posted above is not closed.

2. line 2 and line 5 i see u have tried to test for true or false. this not the way to do it(or so i think, it doesnt work anyway). im sure there is away to explicitly do this, but what ur doing is not the right way anyway, thats where its gone wrong, really.

if ($file=fopen($filename, "ab")

the if loop would execute if the reult was true anyway and else would only execute if it was false,s o as u see, there is no need to explictly do it in your case.

besides, im sure its just me, but never heard of ab as file opening mode.

hope this helps
kumar

mhbweb

2:03 pm on Sep 22, 2004 (gmt 0)

10+ Year Member



thanks loads :):)

your a star!

kumarsena

2:05 pm on Sep 22, 2004 (gmt 0)

10+ Year Member



no probs at all,

by the way, welcome to the community!

:)

kumar