我用它来发送文件给用户
header('Content-type:  application/zip');
header('Content-Length: ' . filesize($file));
header('Content-Disposition: attachment; filename="file.zip"');
readfile($file);
我想在用户下载后删除此文件,我该怎么办?
编辑:我的情况就是这样,当用户点击下载按钮时,我的脚本将创建一个临时zip文件,用户下载它,然后该临时zip文件将被删除.
编辑2:好的最佳方式似乎运行一个cron作业,将每小时清理一次临时文件.
编辑3:我测试了我的脚本unlink,它的工作原理,除非用户取消下载.如果用户取消下载,则zip文件将保留在服务器上.所以这就足够了.:)
编辑4:哇!connection_aborted()成功了!
ignore_user_abort(true);
if (connection_aborted()) {
    unlink($f);
}
即使用户取消下载,也会删除该文件.
ink*_*dmn 30
unlink($filename);
这将删除该文件.
它需要与ignore_user_abort()Docs结合使用,unlink即使用户取消下载,仍然会执行.
ignore_user_abort(true);
...
unlink($f);
小智 12
我总是使用以下解决方案:
register_shutdown_function('unlink', $file);
小智 5
connection_aborted()从来没有为我工作过。虽然ob_clean()完全按照它应该的方式工作。希望这对其他人也有帮助
header('Content-type: application/pdf');
header('Content-Disposition: inline; filename="' . $file . '"');
header('Content-Transfer-Encoding: binary');
header('Accept-Ranges: bytes');
ob_clean();
flush();
if (readfile($file))
{
  unlink($file);
}
| 归档时间: | 
 | 
| 查看次数: | 45429 次 | 
| 最近记录: |