Jump to content

Archived

This topic is now archived and is closed to further replies.

Unholy Prayer

fwrite() and fclose() not valid stream resource?

Recommended Posts

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 17

Warning: fclose(): supplied argument is not a valid stream resource in /home/mtech/public_html/programs/editor.php on line 19
The 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

Share this post


Link to post
Share on other sites
Change your code to:
[code=php:0]// check that the form has been submitted first, before using the POST vars
if(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]

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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.

Share this post


Link to post
Share on other sites
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]<?php
if ($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.

Share this post


Link to post
Share on other sites

×

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.