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?
FWrite
Moderator: General Moderators
fwrite
int fwrite ( resource handle, string string [, int length] )
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
he obviously doesnt know how to use this function so why not make it a little more descriptive and add error handling?
rewritten:
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
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?
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
Fwrite
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.