gnorty
10-14-05, 05:09 PM
HI. I have been fighting all night to save a file to a directory on my site, and keep getting file errors (file not writable).
In desperation, I went back to basics and tried getting a simple example script to work straight from php.net, modifying it (I think) to work on my site. This still is not writing the file correctly. Here is the code I am trying to use, can anyone tell me where it is going wrong? In an effort to persuade the thing to work, the /thumbs directory is set 777.
<?php
$filename = '/www/d/o/domain.com/htdocs/thumbs/test.txt';
$somecontent = "Add this to the file\n";
// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {
// In our example we're opening $filename in append mode.
// The file pointer is at the bottom of the file hence
// that's where $somecontent will go when we fwrite() it.
if (!$handle = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit;
}
// Write $somecontent to our opened file.
if (fwrite($handle, $somecontent) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
echo "Success, wrote ($somecontent) to file ($filename)";
fclose($handle);
} else {
echo "The file $filename is not writable";
}
?>
In desperation, I went back to basics and tried getting a simple example script to work straight from php.net, modifying it (I think) to work on my site. This still is not writing the file correctly. Here is the code I am trying to use, can anyone tell me where it is going wrong? In an effort to persuade the thing to work, the /thumbs directory is set 777.
<?php
$filename = '/www/d/o/domain.com/htdocs/thumbs/test.txt';
$somecontent = "Add this to the file\n";
// Let's make sure the file exists and is writable first.
if (is_writable($filename)) {
// In our example we're opening $filename in append mode.
// The file pointer is at the bottom of the file hence
// that's where $somecontent will go when we fwrite() it.
if (!$handle = fopen($filename, 'a')) {
echo "Cannot open file ($filename)";
exit;
}
// Write $somecontent to our opened file.
if (fwrite($handle, $somecontent) === FALSE) {
echo "Cannot write to file ($filename)";
exit;
}
echo "Success, wrote ($somecontent) to file ($filename)";
fclose($handle);
} else {
echo "The file $filename is not writable";
}
?>