Problems writing data from <textarea> using fwrite() o

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
fallinj
Forum Newbie
Posts: 2
Joined: Mon Mar 03, 2003 11:08 am
Location: Philadelphia, PA

Problems writing data from <textarea> using fwrite() o

Post by fallinj »

I have a php script that reads from an exisitng file, creates an html form with the file contents in a <textarea>, and should write back to the file changes made to the file contents via the html form. This worked fine when I originally tested it on my local Win2000 machine, but not since I have moved the script up to my host's Unix server.

The problem is that only first n number of characters are being saved back to the file when the user submits the form. Not sure exactly what n is but essentially after 20 lines or so (Or sometimes less), data is not being saved to the file.

The HTML form creation is pretty simply. First I read from the file.

Code: Select all

<?php
$filename = "../scripts/$file";
$fp=fopen($filename,"r");
while (!feof ($fp)) {
   $buffer = $buffer . fgets($fp, 4096);
}
$file_data=$buffer;
fclose ($fp);

?>
Then populate the HTML form with $file_data

Code: Select all

<form action="this.php" method="post">
<textarea name="file_data">
<?php print $file_data;?>
</textarea>
<input type="submit">
</form>
When the form is submitted via post method, the script takes the form data and writes to the file.

Code: Select all

$filename = "../scripts/$file";
$filesize=filesize($filename);
$file_data=stripslashes($file_data);

if (is_writable($filename)) {
   if (!$fp = fopen($filename, "w")) {
     print "Cannot open file ($filename)";
     exit;
   }
   if (!fwrite($fp, $file_data, $filesize)) {
       print "Cannot write to file ($filename)";
       exit;
   }
   print "Success, wrote to file ($file)<p>";
   fclose($fp);
} else {
   print "The file $filename is not writable";
}
When this script is run on my local Win2000 machine, it works fine. The entire contents from the <textarea> are written to the file. However when I run this on my host's UNIX machine, only the first n number of characters are written to the file. (I say n number because I can't recognize a pattern. Sometimes more or less of the file contents are written). I never get a PHP error. The script always writes something, just not all of what I want.

How can I write all of the contents from the form to the file? Is there something special that needs to be done with the fwrite() function when running on a Unix file system? Do I need to write to the file line by line?
User avatar
puckeye
Forum Contributor
Posts: 105
Joined: Fri Dec 06, 2002 7:26 pm
Location: Joliette, QC, CA
Contact:

Post by puckeye »

Hi fallinj,

The thing I can see is that you limit the ammount of data you are writing with fwrite.

Take a look at fwrite on PHP.net you'll see that the third value stops writing when n bytes are written.

From your code:

Code: Select all

<?php
   if (!fwrite($fp, $file_data, $filesize)) { 
       print "Cannot write to file ($filename)"; 
       exit; 
?>
Here you fwrite $filesize bytes of data to the file pointed to by $fp.

$filesize is create a little higher in your code :

Code: Select all

<?php
$filename = "../scripts/$file"; 
$filesize=filesize($filename); 
?>
Well what that does is check the filesize of the old file so if there's nothing in that file you'll write nothing to the file because $filename would equal 0. If there's 3000 bytes in the original file then you'll write only the first 3000 bytes of your textearea...

My solution would be to remove the third value of fwrite leaving only fwrite($fp, $file_data)
fallinj
Forum Newbie
Posts: 2
Joined: Mon Mar 03, 2003 11:08 am
Location: Philadelphia, PA

Post by fallinj »

Thanks puckeye,

What a silly mistake. It now makes sense why it seemed to me to only write n number of bytes back to the file. Basically, I could edit the file, and all of the my changes would save as long as the changes were no bigger than the old file.

fallinj
Post Reply