du8828 2014-09-23 04:38
浏览 38
已采纳

在Ob_get_contents()文件覆盖之前提示用户

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> &bull; <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> &bull; <a href="'."/newslettercms/webversion/".$file.'" target="_blank" style="color:#ddd">View web version</a></center></div>';

echo "File overwritten success!";

?>
  • 写回答

1条回答 默认 最新

  • duanqiao0153 2014-09-24 02:22
    关注

    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> &bull; <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!

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 unity第一人称射击小游戏,有demo,在原脚本的基础上进行修改以达到要求
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥15 cmd cl 0x000007b
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)