Jump to content


Photo

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


  • Please log in to reply
7 replies to this topic

#1 Unholy Prayer

Unholy Prayer
  • Members
  • PipPipPip
  • Advanced Member
  • 166 posts

Posted 28 August 2006 - 10:06 AM

Ok, I am trying to make a css editor, and the error message I get on my page is this:

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.

This is my 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";
?>

Can someone please help me?  Here is a link to the page: http://www.mtechdev....rams/editor.php

#2 wildteen88

wildteen88
  • Staff Alumni
  • Advanced Member
  • 10,482 posts
  • LocationUK, Bournemouth

Posted 28 August 2006 - 10:10 AM

Change your code to:
// 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";
}


#3 Unholy Prayer

Unholy Prayer
  • Members
  • PipPipPip
  • Advanced Member
  • 166 posts

Posted 28 August 2006 - 10:23 AM

It's close.  I don't get errors before I click "Edit CSS", but when I click it, I get the same errors.

#4 Satria Ox41464b

Satria Ox41464b
  • Members
  • PipPip
  • Member
  • 18 posts

Posted 28 August 2006 - 10:26 AM

<input type="hidden" name="filename" value="bbultimate.css">
and $_POST['bbultimate.css']

i guess you should put $_POST['filename']

#5 Unholy Prayer

Unholy Prayer
  • Members
  • PipPipPip
  • Advanced Member
  • 166 posts

Posted 28 August 2006 - 11:02 AM

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.

#6 Unholy Prayer

Unholy Prayer
  • Members
  • PipPipPip
  • Advanced Member
  • 166 posts

Posted 29 August 2006 - 03:40 AM

Bump.

#7 DarkendSoul

DarkendSoul
  • Members
  • PipPipPip
  • Advanced Member
  • 121 posts

Posted 29 August 2006 - 06:29 AM

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

<?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!";
}
?>

This is not a permanant solution. Its not a very secure method. You may want to look into a user login.

#8 Yesideez

Yesideez
  • Members
  • PipPipPip
  • Advanced Member
  • 2,334 posts
  • LocationDevon, UK

Posted 29 August 2006 - 07:29 AM

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.
<?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";
  }
}
?>
That way the script would continue even if you were to get the error messages.
Not a pro just an enthusiast :)

if (empty($coffee)) {$coffee=new coffee();}

Please surround any code using the CODE tags - I rarely look at anything without them




0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users