Writing to a text file with php

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
mccommunity
Forum Commoner
Posts: 62
Joined: Mon Oct 07, 2002 8:55 am

Writing to a text file with php

Post 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);


}






?>
Illusionist
Forum Regular
Posts: 903
Joined: Mon Jan 12, 2004 9:32 pm

Post 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!
Gen-ik
DevNet Resident
Posts: 1059
Joined: Mon Aug 12, 2002 7:08 pm
Location: London. UK.

Post 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"
Post Reply