doutan2111 2017-04-06 23:55
浏览 95

尝试生成临时txt文件,然后将其删除

I have a site where I want a user to be able to download some data as a text file, without permanently saving the file on my server. The approach I'm trying is to use JavaScript to send a POST, using PHP to generate and save a text file. On success, JavaScript will open that file in a separate window. After a few seconds delay, it will then send a POST with the file to delete.

I have most of it working, but for some reason, when I try to delete the file, I keep getting an error - No such file or directory. I don't know why, especially since using a test file to delete in the same directory works fine. Here's what I'm using on the javascript side:

////CREATE FILE
    function exportGroup() {

$.post("../Modules/Export_Mod/export_mod.php",
{ submit:'export',
  groupIndex: groupSelect.value,
  userRole: 'admin',
  serial: <?php echo $serial;?>
},
function(data,status){

//open created file in new window  
window.open("../Modules/Export_Mod/"+data);

removeExport(data);
});

};



 //////REMOVE FILE   
    function removeExport(filename) {
///After 1 second, send post to delete file
setTimeout(function() {
            $.post("../Modules/Export_Mod/export_mod.php",
            { submit:'removeExport',
                file: filename


            },
            function(data,status){  
            data;

            });
 }, 1000);

}

and my PHP:

//I'm creating the file successfully with this
...
$filename = $groupName."_group_export.txt"; 
$content = $header.$dataStr; 
$strlength = strlen($content); 
$create = fopen($filename, "w"); 
$write = fwrite($create, $content, $strlength);
$close = fclose($create); 

But when I try to delete a second (or more) later using this:

if (($_POST)&&($_POST['submit']=='removeExport')){

$file = $_POST['file'];

unlink($file);   ///works when using an already-existing file in the same directory ... unlink('test.txt');

}

I get the error. The first thing am wondering is if I'm approaching this the right way. If not, is there a better way to do it? And then the second thing I'm wondering is why I'm getting this error and what I need to change to make it work.

  • 写回答

2条回答 默认 最新

  • dongliuliu0385 2017-04-07 01:31
    关注

    I would check the server permissions to see if the php script that is running is able to delete the files that are created. If you are on a unix based script I would run the

    ls -l /usr/var/
    

    command on the directory that you are storing the files in, to see what permissions have been assigned to them.

    评论

报告相同问题?

悬赏问题

  • ¥17 pro*C预编译“闪回查询”报错SCN不能识别
  • ¥15 微信会员卡接入微信支付商户号收款
  • ¥15 如何获取烟草零售终端数据
  • ¥15 数学建模招标中位数问题
  • ¥15 phython路径名过长报错 不知道什么问题
  • ¥15 深度学习中模型转换该怎么实现
  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向