Jump to content

fread() and fclose() problems


marcus

Recommended Posts

Ok, I'm getting errors with the fread and fclose functions.

[code]
<?php
$loadcontent = "$_GET[page]";
if($save_file) {
$savecontent = stripslashes($savecontent);
$fp = @fopen($loadcontent, "w");
if ($fp) {
fwrite($fp, $savecontent);
fclose($fp);
}
}
$fp = @fopen($loadcontent, "r");
$loadcontent = fread($fp, filesize($loadcontent));
$loadcontent = htmlspecialchars($loadcontent);
fclose($fp);

?>
<form method=post action="<?=$_SERVER[PHP_SELF]?>">
<textarea name="savecontent" cols="70" rows="25"><?=$loadcontent?></textarea>
<br>
<input type="submit" name="save_file" value="Save">
</form>
[/code]

I get the errors:

[code]

Warning: fread(): supplied argument is not a valid stream resource in /home/neoblob/public_html/php/3/edit.php on line 12

Warning: fclose(): supplied argument is not a valid stream resource in /home/neoblob/public_html/php/3/edit.php on line 14
[/code]
Link to comment
Share on other sites

It a coding problem, your relying on registered globals being turn on, so that you can do stuff that is bad by design. Sure all us want to get the code to do what we want, but the dangerous risks created by doing things the easy way is never worth that risk!


[code]<?php

// were is $_GET['page'] being set, also quote your array elements

/* $loadcontent = "$_GET[page]"; */

// the above should be written like... (but also add validation, if you don't you will get a error [undefined variable 'page']

$loadcontent = $_GET['page'];

// your counting on registered globals, don't do that, also never use a submit button as testing variable, people don't always use it, so it might not get set!

/* if($save_file) { */

// better to do

if ( isset ( $_POST['savecontent'] ) && trim ( $_POST['savecontent'] ) != '' )
{

// again, your counting on registered globals, don't do that, also what's the stripslashes for? Use it only if magic quotes is on. Also no need to copy a variable that already exists, your wasting memory and doing extra unneeded stuff!

/* $savecontent = stripslashes($savecontent); */

// if you must copy it, then all you need to do is!

$content = $_POST['savecontent'];

$fp = @fopen ( $loadcontent, 'w' );
if ( $fp )
{
fwrite ( $fp, $content );
fclose ( $fp );
}
}
else // add else here, so you only read when you need to
{
$fp = @fopen ( $loadcontent, 'r' );
$content = htmlspecialchars ( fread ( $fp, filesize ( $loadcontent ) ) );
fclose ( $fp );
}

?>
<form method=post action="<?=$_SERVER['PHP_SELF'];?>">
<textarea name="savecontent" cols="70" rows="25"><?=$content;?></textarea>
<br>
<input type="submit" name="save_file" value="Save">
</form>[/code]


me!
Link to comment
Share on other sites

Like I said...

Where is $_GET['page'], being set, that is causing your error! You can't write to a file handle if it doesn't have a name, if $_GET['page'] doesn't get set, then you never have a file name that fopen() needs to open or create the file to put your $_POST['savecontent'] in to!


me!
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.