Jump to content


Photo

fread() and fclose() problems


  • Please log in to reply
3 replies to this topic

#1 marcus

marcus
  • Members
  • PipPipPip
  • Advanced Member
  • 1,842 posts
  • LocationRochester, NY

Posted 16 October 2006 - 01:32 AM

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

<?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>

I get the errors:


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


#2 printf

printf
  • Staff Alumni
  • Advanced Member
  • 889 posts

Posted 16 October 2006 - 01:45 AM

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!


<?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>


me!

#3 marcus

marcus
  • Members
  • PipPipPip
  • Advanced Member
  • 1,842 posts
  • LocationRochester, NY

Posted 16 October 2006 - 01:50 AM

it still doesnt save it :(

#4 printf

printf
  • Staff Alumni
  • Advanced Member
  • 889 posts

Posted 16 October 2006 - 01:59 AM

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!




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users