Jump to content

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


Unholy Prayer

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
Link to comment
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]
Link to comment
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.
Link to comment
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.
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.