Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/255.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在Ob_get_contents()文件覆盖之前提示用户_Php_Jquery_Ajax_Overwrite_Ob Get Contents - Fatal编程技术网

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

Php 在Ob_get_contents()文件覆盖之前提示用户,php,jquery,ajax,overwrite,ob-get-contents,Php,Jquery,Ajax,Overwrite,Ob Get Contents,我在PHP中使用file_put_contents($file,ob_get_contents())函数创建动态表单生成的文件快照,并将文件内容存储在服务器中。它工作得很好,但如果已经有同名文件,我想提示用户是否要覆盖。使用AJAX,我可以通过隐藏的输入字段传递文件名,但无法传递文件内容。如果还有其他更简单的选择,我不需要AJAX 此处:$file是表单生成的文件名 if(file_exists($file)) { echo '<div style="background:#000

我在PHP中使用file_put_contents($file,ob_get_contents())函数创建动态表单生成的文件快照,并将文件内容存储在服务器中。它工作得很好,但如果已经有同名文件,我想提示用户是否要覆盖。使用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>
if(文件存在($file))
{
echo“文件区域存在!”;
回声“覆盖它!”;
}
否则{
文件内容($file,ob\u get\u contents());
回声“&bull;”;
}
函数loadXMLDoc()
{
var-xmlhttp;
if(window.XMLHttpRequest)
{//IE7+、Firefox、Chrome、Opera、Safari的代码
xmlhttp=新的XMLHttpRequest();
}
其他的
{//IE6、IE5的代码
xmlhttp=新的ActiveXObject(“Microsoft.xmlhttp”);
}
xmlhttp.onreadystatechange=函数()
{
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();
}
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!";

?>

使用非常简单的Wavemode概念,我使用AJAX get请求解决了这个问题。我必须使用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>

如果有人能为这个问题提供更好的解决方案,我会很感兴趣。干杯

我看到的最简单的方法是,如果文件存在,只需将文件保存为
$file'temp'
,然后提示用户是否要用临时文件覆盖原始文件。为什么此解决方案不起作用?@kasimir,因为AJAX get请求无法将内容传递到overwrite.php文件。@wavemode非常感谢。我用你的想法解决了这个问题。都整理好了。我会很快发布解决方案。
$newfilename=$_GET['newfilename'];
$oldfilename=$_GET['oldfilename'];

rename($newfilename,$oldfilename);