I am using file_put_contents($file, ob_get_contents()) function in PHP to create a snapshot of file generated by a dynamic form and store the contents of the file in server. It is working quite well but if there is file already with the same name, I want to prompt user asking if want to overwrite. With AJAX, I could pass the name of the file via hidden input field but couldn't pass the contents of the file. I don't need AJAX if there are other simpler alternatives.
Here: $file is the name of the file generated by the form.
if(file_exists($file))
{
echo '<div style="background:#000; padding:10px"><center style="color:#fff">File aready exists! ';
echo '<button type="button" onclick="loadXMLDoc()">Overwrite it!</button><div id="myDiv"></div></div><input type="hidden" id="hiddenfile" value="'.$file.'"></center>';
}
else{
file_put_contents($file, ob_get_contents());
echo '<div style="background:#000; padding:10px"><center><a href="'."/newslettercms/webversion/".$file.'" download style="color:#fff;">Click here to download the newsletter</a> • <a href="'."/newslettercms/webversion/".$file.'" target="_blank" style="color:#ddd">View web version</a></center></div>';
}
<script>
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
var hiddenfile=document.getElementById("hiddenfile").value;
xmlhttp.open("GET","inc/overwrite.php?hiddenfile="+hiddenfile,true);
xmlhttp.send();
}
</script>
Overwrite.php
<?php
$file=$_GET['hiddenfile'];
file_put_contents($file, ob_get_contents());
echo '<div style="background:#000; padding:10px"><center><a href="'."/newslettercms/webversion/".$file.'" download style="color:#fff;">Click here to download the newsletter</a> • <a href="'."/newslettercms/webversion/".$file.'" target="_blank" style="color:#ddd">View web version</a></center></div>';
echo "File overwritten success!";
?>
Using very simple concept of Wavemode, I fixed this issue using AJAX get request. I have to use AJAX or the whole page would refresh and the dynamically generated contents would be lost. I was in the right track to use AJAX in here, however I was trying to overwrite on the fly using file_put_contents(). Here is the code how I fixed this issue.
if(file_exists($file))
{
$file2="temp-".$file;
file_put_contents($file2, ob_get_contents());
echo '<div style="background:#000; padding:10px"><center style="color:#fff">File aready exists! ';
echo '<button type="button" onclick="loadXMLDoc()">Overwrite it!</button><div id="myDiv"></div></div><input type="hidden" id="newfilename" value="'.$file2.'"><input type="hidden" id="oldfilename" value="'.$file.'"></center>';
}
else{
file_put_contents($file, ob_get_contents());
echo '<div style="background:#000; padding:10px"><center><a href="'."/newslettercms/webversion/".$file.'" download style="color:#fff;">Click here to download the newsletter</a> • <a href="'."/newslettercms/webversion/".$file.'" target="_blank" style="color:#ddd">View web version</a></center></div>';
}
?>
<script>
function loadXMLDoc()
{
var xmlhttp;
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
document.getElementById("myDiv").innerHTML=xmlhttp.responseText;
}
}
var oldfilename=document.getElementById("oldfilename").value;
var newfilename=document.getElementById("newfilename").value;
xmlhttp.open("GET","inc/overwrite.php?oldfilename="+oldfilename+"&newfilename="+newfilename,true);
xmlhttp.send();
}
</script>
Overwrite.php
$newfilename=$_GET['newfilename'];
$oldfilename=$_GET['oldfilename'];
rename($newfilename,$oldfilename);
I would be interested if anyone provide better solution to this issue. Cheers!