marcus Posted October 16, 2006 Share Posted October 16, 2006 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 12Warning: fclose(): supplied argument is not a valid stream resource in /home/neoblob/public_html/php/3/edit.php on line 14[/code] Quote Link to comment Share on other sites More sharing options...
printf Posted October 16, 2006 Share Posted October 16, 2006 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 doif ( 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! Quote Link to comment Share on other sites More sharing options...
marcus Posted October 16, 2006 Author Share Posted October 16, 2006 it still doesnt save it :( Quote Link to comment Share on other sites More sharing options...
printf Posted October 16, 2006 Share Posted October 16, 2006 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! Quote Link to comment Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.