...

View Full Version : PHP File Write Error



blackfish
04-02-2007, 04:56 PM
Hi, i have a php code script running on my website, the problem i have is that it will not write data to the text file even though it exists?

error is:

The file words.txt is not writable

script below:

<?php
$filename = 'words.txt';
$somecontent = $_GET['words'];

// 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";
}
?>


Can anyone help me?

BlackFish

neel_basu
04-02-2007, 05:08 PM
Try this
<?php
$filename = 'words.txt';
$somecontent = $_GET['words'];

// Let's make sure the file exists and is writable first.
if(!is_writable($filename))
{
chmod($filename, 777);
}
else{

// 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";
}
?>

mlseim
04-02-2007, 06:16 PM
Also check this ...
Some webhosts have disabled fopen,fwrite for security reasons.
Make sure your webhost has it enabled.



EZ Archive Ads Plugin for vBulletin Copyright 2006 Computer Help Forum