2014-09-23 43 views
0

我使用file_put_contents($文件,ob_get_contents())函数在PHP创建一个动态表单生成的文件的快照,并存储在服务器上的文件的内容。它工作得很好,但如果有已经有相同名称的文件,我想提示用户询问是否要覆盖。使用AJAX,我可以通过隐藏的输入字段传递文件的名称,但无法传递文件的内容。如果还有其他更简单的选择,我不需要AJAX。提示用户覆盖

这里: $ file是表单生成的文件的名称。

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!"; 

?> 
+0

我看到的是最简单的方法,如果该文件存在,只需将文件保存为'$文件。 'temp',然后提示用户是否要用临时文件覆盖原件。 – wavemode 2014-09-23 04:44:29

+0

为什么此解决方案无法正常工作? – kasimir 2014-09-23 08:08:17

+0

@kasimir怎么把AJAX get请求无法将内容传递给overwrite.php文件。 – floCoder 2014-09-24 02:15:22

回答

1

使用Wavemode的非常简单的概念,我固定使用AJAX请求得到这个问题。我必须使用AJAX,否则整个页面都会刷新,动态生成的内容将会丢失。我在这里使用AJAX的正确轨道,但我试图使用file_put_contents()在飞行中覆盖。这是我如何解决这个问题的代码。

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); 

我有兴趣,如果有人提供更好的解决这个问题。干杯!