How to Make this script rewrite content in Output Files , Now it's only merge a new content with a previous content (in the same files)
Here is my code
<?php
$filename = 'test.php';
$somecontent = "<?php $jdst_xx = 'HELLO'; ?>
";
// 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";
}
?>
try to change your
if (!$handle = fopen($filename, 'a')) { // open in append mode
to
if (!$handle = fopen($filename, 'w')) { // open in write mode
For more :- http://www.php.net/manual/en/function.fwrite.php
You're using append mode here:
if (!$handle = fopen($filename, 'a')) {
If you want to completely overwrite the file, simply change to
if (!$handle = fopen($filename, 'w')) {
if you want to just overwrite it.
Hope I helped :)
See that little 'a' in this line?
fopen($filename, 'a')
Well, that means append. Check out the documentation on php.net for fopen. What do you think should go there instead of 'a'?
Try this:
if (!$handle = fopen($filename, 'w'))
w - Open for writing only; place the file pointer at the beginning of the file and truncate the file to zero length. If the file does not exist, attempt to create it.
more can be found here: http://www.php.net/manual/en/function.fopen.php
To do an overwrite of the file you have to open the file passing 'W' instead of 'a'. Like shown below.
if (!$handle = fopen($filename, 'W')) {
echo "Cannot open file ($filename)";
exit;
}
Hope this helps!