Problems writing data from <textarea> using fwrite() o
Posted: Mon Mar 03, 2003 11:08 am
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.
Then populate the HTML form with $file_data
When the form is submitted via post method, the script takes the form data and writes to the file.
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?
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);
?>Code: Select all
<form action="this.php" method="post">
<textarea name="file_data">
<?php print $file_data;?>
</textarea>
<input type="submit">
</form>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";
}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?