Page 1 of 1

FWrite

Posted: Tue May 17, 2005 6:24 am
by vivekjain
Hi,
I am getting this error, while trying to write to a file:

Warning: fwrite(): supplied argument is not a valid stream resource in c:\inetpub\wwwroot\


Can anyone suggest a solution?

Posted: Tue May 17, 2005 7:04 am
by anjanesh
fwrite
int fwrite ( resource handle, string string [, int length] )

Code: Select all

$somecontent = "Add this to the file\n";
$handle = fopen("test.txt", 'a');
fwrite($handle, $somecontent);

Posted: Tue May 17, 2005 7:56 am
by malcolmboston
he obviously doesnt know how to use this function so why not make it a little more descriptive and add error handling?

rewritten:

Code: Select all

<?php
$filename = 'test.txt';
$somecontent = "Some content here\n";
// Check if its writable first
if (is_writable($filename))
{
   // attempt to open in write mode (w) 
   if (!$dh = fopen($filename, 'w'))
      {
         print "Unable to open file: {$filename}";
         exit;
      }
   // Write $somecontent to our opened file.
   if (fwrite($dh, $somecontent) === FALSE)
   {
      print "Unable to write to file: ($filename)";
      exit;
   }
   
   echo "Successfully written ($somecontent) to file ($filename)";
   // close directory handle
   fclose($dh);
}
else
{
   prnt "Unable to write to file: {$filename}";
}
?>

FWrite Error

Posted: Tue May 17, 2005 8:06 am
by vivekjain
I tried the code and it doesnt create the file, and it says

Unable to write to file: test.txt

Is there any folder permissions that need to be set?

Posted: Tue May 17, 2005 8:14 am
by malcolmboston
create the file first 8O

Posted: Tue May 17, 2005 8:27 am
by anjanesh
Set write persmission to the folder (CHMOD 777 or right-click in most ftp programs and give persmission to all 9 modes).
Or just create a blank file and give permission (writing) to that file alone.

Fwrite

Posted: Tue May 17, 2005 11:58 pm
by vivekjain
The problem is, by default the folder is in read only mode. But when I try to change the folder mode, I deselect Read Only, but once that is done I check the mode and it yet is in read only mode. Not too sure if this is an OS issue. I am using XP Professional.