加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 站长学院 > PHP教程 > 正文

在php下载后从服务器中删除文件

发布时间:2020-12-13 16:26:18 所属栏目:PHP教程 来源:网络整理
导读:我正在使用php下载文件,我希望文件在成功完成下载后自动从服务器中删除.我在php中使用这个代码. $fullPath = 'folder_name/download.txt';if ($fd = fopen ($fullPath,"r")) { $fsize = filesize($fullPath); $path_parts = pathinfo($fullPath); $ext = str
我正在使用php下载文件,我希望文件在成功完成下载后自动从服务器中删除.我在php中使用这个代码.
$fullPath = 'folder_name/download.txt';

if ($fd = fopen ($fullPath,"r")) {
    $fsize = filesize($fullPath);
    $path_parts = pathinfo($fullPath);
    $ext = strtolower($path_parts["extension"]);

    header("Content-type: application/octet-stream");
    header("Content-Disposition: filename="".$path_parts["basename"].""");
    header("Content-length: $fsize");
    header("Cache-control: private"); //use this to open files directly
    $fd = fopen ($fullPath,"r");
    while(!feof($fd)) {
        $buffer = fread($fd,2048);
        echo $buffer;
    }
    fclose ($fd);

}

unlink($fullPath);

您可以在下载后的代码中看到我取消链接文件.但是如果我这样做损坏文件正在下载.因为有一段时间,文件在被完全下载之前被删除.有没有在php知道客户端下载文件成功,那么我可以删除吗?任何想法都将非常感激.

没有办法知道用户何时用PHP完成文件下载,我会使用队列系统在请求的n秒后删除文件:

How to Build a PHP Queue System

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读