FWrite

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
vivekjain
Forum Commoner
Posts: 76
Joined: Thu Jan 08, 2004 12:38 am

FWrite

Post 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?
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post 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);
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post 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}";
}
?>
vivekjain
Forum Commoner
Posts: 76
Joined: Thu Jan 08, 2004 12:38 am

FWrite Error

Post 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?
malcolmboston
DevNet Resident
Posts: 1826
Joined: Tue Nov 18, 2003 1:09 pm
Location: Middlesbrough, UK

Post by malcolmboston »

create the file first 8O
User avatar
anjanesh
DevNet Resident
Posts: 1679
Joined: Sat Dec 06, 2003 9:52 pm
Location: Mumbai, India

Post 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.
vivekjain
Forum Commoner
Posts: 76
Joined: Thu Jan 08, 2004 12:38 am

Fwrite

Post 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.
Post Reply