Unholy Prayer Posted August 28, 2006 Share Posted August 28, 2006 Ok, I am trying to make a css editor, and the error message I get on my page is this:[code]Warning: fwrite(): supplied argument is not a valid stream resource in /home/mtech/public_html/programs/editor.php on line 17Warning: fclose(): supplied argument is not a valid stream resource in /home/mtech/public_html/programs/editor.php on line 19The CSS has been updated.[/code]This is my code:[code]<?php $filecontent = file_get_contents("bbultimate.css");?><form method="post" action="editor.php"><input type="hidden" name="filename" value="bbultimate.css"> <br><textarea name="theText" cols="50" rows="15"><?php echo "$filecontent"; ?></textarea> <br><input type="submit" value="Edit CSS"></form> <?php$filename = $_POST["bbultimate.css"];$theText = $_POST["theText"];$theText = stripslashes($theText);$data = fopen($filename, "a");fwrite($data,$theText);fclose($data);echo "The CSS has been edited";?>[/code]Can someone please help me? Here is a link to the page: http://www.mtechdev.com/programs/editor.php Quote Link to comment https://forums.phpfreaks.com/topic/18883-fwrite-and-fclose-not-valid-stream-resource/ Share on other sites More sharing options...
wildteen88 Posted August 28, 2006 Share Posted August 28, 2006 Change your code to:[code=php:0]// check that the form has been submitted first, before using the POST varsif(isset($_POST['theText'])){ $filename = $_POST["bbultimate.css"]; $theText = $_POST["theText"]; $theText = stripslashes($theText); $data = fopen($filename, "a"); fwrite($data,$theText); fclose($data); echo "The CSS has been edited";}[/code] Quote Link to comment https://forums.phpfreaks.com/topic/18883-fwrite-and-fclose-not-valid-stream-resource/#findComment-81531 Share on other sites More sharing options...
Unholy Prayer Posted August 28, 2006 Author Share Posted August 28, 2006 It's close. I don't get errors before I click "Edit CSS", but when I click it, I get the same errors. Quote Link to comment https://forums.phpfreaks.com/topic/18883-fwrite-and-fclose-not-valid-stream-resource/#findComment-81534 Share on other sites More sharing options...
Satria Ox41464b Posted August 28, 2006 Share Posted August 28, 2006 <input type="hidden" name="filename" value="bbultimate.css">and $_POST['bbultimate.css'] i guess you should put $_POST['filename'] Quote Link to comment https://forums.phpfreaks.com/topic/18883-fwrite-and-fclose-not-valid-stream-resource/#findComment-81535 Share on other sites More sharing options...
Unholy Prayer Posted August 28, 2006 Author Share Posted August 28, 2006 Ok, there's no error now, but it doesn't change the file contents. Lol, now it's displaying the CSS in the text area like 10 times. Quote Link to comment https://forums.phpfreaks.com/topic/18883-fwrite-and-fclose-not-valid-stream-resource/#findComment-81541 Share on other sites More sharing options...
Unholy Prayer Posted August 29, 2006 Author Share Posted August 29, 2006 Bump. Quote Link to comment https://forums.phpfreaks.com/topic/18883-fwrite-and-fclose-not-valid-stream-resource/#findComment-82013 Share on other sites More sharing options...
DarkendSoul Posted August 29, 2006 Share Posted August 29, 2006 Now before you change this... you have to secure you session... atleast a little...change it to this** ADDITIONAL MODIFICATIONS!Also you may want to keep track of what its doing...[code=php:0]<?php// Change this to your own password.// Now access your page by using http://www.mtechdev.com/programs/editor.php?pass=YOUR_PASS_HERE$password = 'YOUR_PASS_HERE';if ($_GET['pass'] == $password) { $filecontent = file_get_contents("bbultimate.css");// REMOVED "<input type="hidden" name="filename" value="bbultimate.css">" pointless.?><form method="post" action="editor.php?pass=<?echo $_GET['pass']?>"><br><textarea name="theText" cols="50" rows="15"><?php echo "$filecontent"; ?></textarea> <br><input type="submit" value="Edit CSS"></form> <?php // check that the form has been submitted first, before using the POST vars if(isset($_POST['theText'])) { // PROBLEM WAS HERE; BEFORE "$_POST["bbultimate.css"]" $filename = "bbultimate.css"; $theText = $_POST["theText"]; $theText = stripslashes($theText); if (is_writable($filename)) { if (!$data = fopen($filename, 'w')) { echo "Cannot open file ($filename)"; exit; } if (fwrite($data, $theText) === FALSE) { echo "Cannot write to file ($filename)"; exit; } fclose($data); echo "The CSS has been edited"; } else { echo "The file $filename is not writable"; } }}else { echo "Get access biatch!";}?>[/code]This is not a permanant solution. Its not a very secure method. You may want to look into a user login. Quote Link to comment https://forums.phpfreaks.com/topic/18883-fwrite-and-fclose-not-valid-stream-resource/#findComment-82030 Share on other sites More sharing options...
Yesideez Posted August 29, 2006 Share Posted August 29, 2006 Now DarkendSoul's code is ok but I never use "exit" as this will terminate the script whenever something fails and its best to let the script continue.[code]<?phpif ($data=fopen($filename,'w')) { if (fwrite($data,$theText)===false) { echo "ERROR: Cannot write to file"; } else { fclose($data); // DATA WRITTEN OK, FILE CLOSED OK, CONTINUE CODE HERE } else { echo "ERROR: Unable to open file"; }}?>[/code]That way the script would continue even if you were to get the error messages. Quote Link to comment https://forums.phpfreaks.com/topic/18883-fwrite-and-fclose-not-valid-stream-resource/#findComment-82045 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.