Page 1 of 1

Writing to a text file with php

Posted: Fri Jan 23, 2004 1:37 pm
by mccommunity
I am trying to open a text file and modify it then save it to the server What am I doing wrong? This does not work it give me errors, I did set the permissions on the file and directory to chmod 777:




Warning: fwrite(): supplied argument is not a valid stream resource in /usr/www/users/development/php/text editor/test.php on line 40

Warning: fclose(): supplied argument is not a valid stream resource in /usr/www/users/development/php/text editor/test.php on line 41




<html>

<?php
$filename="text.txt";

// Gets webpage into a string
$html = implode('', file('text.txt'));
echo $html;


?>


<body>
<form name="form1" method="post" action="test.php">
<textarea name="newfile" cols="30" rows="10"><? echo $html; ?></textarea>
<input type="submit" name="submit" value="submit">
</form>




</html>


<?

if ($submit)
{
$file = "$filename";
$fh = fopen($file, 'w+');
fwrite($fh, $newfile);
fclose($fh);


}






?>

Posted: Fri Jan 23, 2004 10:08 pm
by Illusionist
this may not be it, but instead of creating $file just use $filename when opeing the file.... and change the 'w+' to "w+"

I don't know if any of that will help, but its worth a try!

Posted: Sat Jan 24, 2004 8:41 am
by Gen-ik

Code: Select all

<?php
$file = $filename;
$fh = fopen($file, 'w+');
fwrite($fh, $newfile);
fclose($fh);
?>
$newfile being sent from the form so try using $_POST["newfile"] instead of $newfile.

If you are just writing to a file then you can use "w" instead of "w+" and also remember that if you are using a windows system then you need to add "b" to the end of it... ie "wb" or "w+b"