在用户下载后删除文件

时间:2021-03-14 00:03:26

I am using this for sending file to user

我正在用它向用户发送文件

header('Content-type:  application/zip');
header('Content-Length: ' . filesize($file));
header('Content-Disposition: attachment; filename="file.zip"');
readfile($file);

I want to delete this file after user downloads it, how can i do this?

我想在用户下载后删除这个文件,怎么做呢?

EDIT: My scenario is like that, when user hits download button, my script will create a temporary zip file and user download it then that temp zip file will be deleted.

编辑:我的场景是这样的,当用户点击下载按钮时,我的脚本将创建一个临时zip文件并用户下载它,然后那个临时zip文件将被删除。

EDIT2: OK best way seems running a cron job that will be cleaning temp files once an hour.

好的,最好的方法是运行cron作业,每小时清理一次临时文件。

EDIT3: I tested my script with unlink, it works unless user cancel the download. If user cancel the download, zip file stays on the server. So that is enough for now. :)

我用unlink测试了我的脚本,除非用户取消下载,否则它可以运行。如果用户取消了下载,zip文件将留在服务器上。这就足够了。:)

EDIT4: WOW! connection_aborted() made the trick !

标:哇!connection_aborted()很有用!

ignore_user_abort(true);
if (connection_aborted()) {
    unlink($f);
}

This one will delete the file even if user cancel the download.

这个将删除文件,即使用户取消下载。

7 个解决方案

#1


21  

unlink($filename);

This will delete the file.

这将删除该文件。

It needs to be combined with ignore_user_abort()Docs so that the unlink is still executed even the user canceled the download.

它需要与ignore_user_abort()文档结合,这样即使用户取消了下载,unlink仍然会被执行。

ignore_user_abort(true);

...

unlink($f);

#2


10  

I always use the following solution:

我总是使用以下的解决方案:

register_shutdown_function('unlink', $file);

#3


8  

There is no any correct way to detect whether file was completely downloaded by user or not.
So the best way will be to delete file after some period of inactivity.

没有任何正确的方法检测文件是否被用户完全下载。所以最好的方法就是在一段时间内删除文件。

#4


3  

I too have very similar functionality in one of my website. It will be like deleting randomly created folder & zip file after/cancelling download. I try to explain it here, may be someone find it useful.

我的一个网站也有类似的功能。它将会像删除随机创建的文件夹和zip文件后/取消下载。我试着在这里解释一下,也许有人会觉得它有用。

skin.php:
This page contains download link such as "http://mysite.com/downoload/bluetheme"

的皮肤。php:此页面包含下载链接,如“http://sitemy.com/downoload/bluetheme”

.htaccess:
I have following rule in htaccess file to redirect the download request to a php file. [download.php].

.htaccess:我遵循htaccess文件中的规则,将下载请求重定向到一个php文件。[download.php]。

RewriteRule ^download/([A-Za-z0-9]+)$ download.php?file=$1 [L]

download.php:

download.php:

include "class.snippets.php";
$sn=new snippets();
$theme=$_GET['file'];
$file=$sn->create_zip($theme);
$path="skins/tmp/$file/$file.zip";
$config_file="skins/tmp/$file/xconfig.php";
$dir="skins/tmp/$file";

$file.=".zip";
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=$file");
header("Pragma: no-cache");
header("Expires: 0");
readfile($path);

//remove file after download  
unlink($path);
unlink($config_file);
rmdir($dir);

so on request download.php will create directory with a random name using snippets class. Inside the directory it will create a zip file. after the download/cancelling the request all the files and the directory will be deleted.

请求下载。php将使用snippet类创建具有随机名称的目录。在目录中,它将创建一个zip文件。下载/取消请求后,所有文件和目录将被删除。

#5


2  

I couldn't find something which worked for me, so I came up with this, which seems to work well for me:

我找不到对我有用的东西,所以我想到了这个,似乎对我很有用:

header('Content-type: application/zip'); //this could be a different header 
header('Content-Disposition: attachment; filename="'.$zipName.'"');

ignore_user_abort(true);

$context = stream_context_create();
$file = fopen($zipName, 'rb', FALSE, $context);
while(!feof($file))
{
    echo stream_get_contents($file, 2014);
}
fclose($file);
flush();
if (file_exists($zipName)) {
    unlink( $zipName );
}

I hope that helps someone

我希望这能帮助某人

#6


0  

The safest way I can think of would be to use some client-side flash movie that can observe the transfer from the client side, and then make an XmlHttpRequest call to the server once the download finishes.

我能想到的最安全的方法是使用一些客户端flash影片,它可以观察从客户端进行的传输,然后在下载完成后对服务器进行XmlHttpRequest调用。

AFAIK, there's no way to do that reliably without using Flash (or a similar browser plugin)

如果不使用Flash(或类似的浏览器插件),就无法可靠地实现这一点

#7


0  

connection_aborted() never worked for me. But ob_clean() works exactly as it should. hope this help others too

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

#1


21  

unlink($filename);

This will delete the file.

这将删除该文件。

It needs to be combined with ignore_user_abort()Docs so that the unlink is still executed even the user canceled the download.

它需要与ignore_user_abort()文档结合,这样即使用户取消了下载,unlink仍然会被执行。

ignore_user_abort(true);

...

unlink($f);

#2


10  

I always use the following solution:

我总是使用以下的解决方案:

register_shutdown_function('unlink', $file);

#3


8  

There is no any correct way to detect whether file was completely downloaded by user or not.
So the best way will be to delete file after some period of inactivity.

没有任何正确的方法检测文件是否被用户完全下载。所以最好的方法就是在一段时间内删除文件。

#4


3  

I too have very similar functionality in one of my website. It will be like deleting randomly created folder & zip file after/cancelling download. I try to explain it here, may be someone find it useful.

我的一个网站也有类似的功能。它将会像删除随机创建的文件夹和zip文件后/取消下载。我试着在这里解释一下,也许有人会觉得它有用。

skin.php:
This page contains download link such as "http://mysite.com/downoload/bluetheme"

的皮肤。php:此页面包含下载链接,如“http://sitemy.com/downoload/bluetheme”

.htaccess:
I have following rule in htaccess file to redirect the download request to a php file. [download.php].

.htaccess:我遵循htaccess文件中的规则,将下载请求重定向到一个php文件。[download.php]。

RewriteRule ^download/([A-Za-z0-9]+)$ download.php?file=$1 [L]

download.php:

download.php:

include "class.snippets.php";
$sn=new snippets();
$theme=$_GET['file'];
$file=$sn->create_zip($theme);
$path="skins/tmp/$file/$file.zip";
$config_file="skins/tmp/$file/xconfig.php";
$dir="skins/tmp/$file";

$file.=".zip";
header("Content-type: application/zip");
header("Content-Disposition: attachment; filename=$file");
header("Pragma: no-cache");
header("Expires: 0");
readfile($path);

//remove file after download  
unlink($path);
unlink($config_file);
rmdir($dir);

so on request download.php will create directory with a random name using snippets class. Inside the directory it will create a zip file. after the download/cancelling the request all the files and the directory will be deleted.

请求下载。php将使用snippet类创建具有随机名称的目录。在目录中,它将创建一个zip文件。下载/取消请求后,所有文件和目录将被删除。

#5


2  

I couldn't find something which worked for me, so I came up with this, which seems to work well for me:

我找不到对我有用的东西,所以我想到了这个,似乎对我很有用:

header('Content-type: application/zip'); //this could be a different header 
header('Content-Disposition: attachment; filename="'.$zipName.'"');

ignore_user_abort(true);

$context = stream_context_create();
$file = fopen($zipName, 'rb', FALSE, $context);
while(!feof($file))
{
    echo stream_get_contents($file, 2014);
}
fclose($file);
flush();
if (file_exists($zipName)) {
    unlink( $zipName );
}

I hope that helps someone

我希望这能帮助某人

#6


0  

The safest way I can think of would be to use some client-side flash movie that can observe the transfer from the client side, and then make an XmlHttpRequest call to the server once the download finishes.

我能想到的最安全的方法是使用一些客户端flash影片,它可以观察从客户端进行的传输,然后在下载完成后对服务器进行XmlHttpRequest调用。

AFAIK, there's no way to do that reliably without using Flash (or a similar browser plugin)

如果不使用Flash(或类似的浏览器插件),就无法可靠地实现这一点

#7


0  

connection_aborted() never worked for me. But ob_clean() works exactly as it should. hope this help others too

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