Reputation: 61
Im trying to write a file in php with the following code
$filename = "uploads/stories/".$row['s_source'];
$handle = fopen($filename, "r");
if (filesize($filename) == 0) {
$txt = "{====views====}{====likes====}{====chapters====}{====comments====}";
fwrite($handle, $txt);
}
$content = fread($handle, filesize($filename));
fclose($handle);
And I get:
Notice: fwrite(): write of 66 bytes failed with errno=9 Bad file descriptor in E:\xampp\htdocs\host\host\storyedit.php on line 20
Warning: fread(): Length parameter must be greater than 0 in E:\xampp\htdocs\host\host\storyedit.php on line 22
Notice: Undefined offset: 1 in E:\xampp\htdocs\host\host\storyedit.php on line 27
But there is no error (I think) in the code. Could someone help me?
I've tried:
none worked.
Upvotes: 3
Views: 23198
Reputation: 1
For anybody having this error, it can be as well just opening file with mode 'a'. Using mode 'r' to read files is the right way.
Upvotes: 0
Reputation: 591
You open with reading access r
and then you give a write command >
. You need to open with read/write access:
$handle = fopen($filename, "r+");
In relation to error in fread
, try:
if (filesize($filename) > 0) {
$content = fread($handle, filesize($filename));
}
Upvotes: 8