web服务器如何判断客户端文件已下载结束?(php实现)
对于web服务器端如何判断客户端文件已下载结束?
下面根据本人的实践,经过整理总结,与php爱好者共同交流,由于本人水平还是有限,如有不到之处,请各位php编程高手提出更好的建议!
代码如下:
<?
//定义下载函数
function DownloadAuth($Path,$DownFile,$isDeleteFile = false)
{
$downloadfile = $Path."/".$DownFile;
if (!file_exists($downloadfile)) {
return -1;
}
// 打开文件
$fd = fopen($downloadfile,"r");
//输入文件标签
Header("Content-type: application/octet-stream");
header("Accept-Ranges: bytes");
header("Accept-Length: ".filesize($downloadfile));
Header("Content-Disposition: attachment; filename=$DownFile");
//printf("开始下载...<br>");
// 输出文件内容
//采用以下两行代码,如果客户端没有下载完成时单击取消按钮,则后面的文件无法正常删除
while (!feof ($fd))
{ echo fread($fd,50000); }
//如何要使用户在文件没下载结束单击“取消”按钮,下载的文件要能正常删除下载的文件,将代码改成下面:
/*
echo fread($fd,filesize($downloadfile);
*/
fclose ($fd);
if ($isDeleteFile)
unlink($downloadfile);
return 0;
}
//客户端下载文件
$ret = downloadauth("./","a.txt");
/*if ( $ret == 0 )
printf("下载文件成功");
else
printf("下载失败");*/
?>
- 上一篇: 使用Thinkphp自带HTTP类的功能实现文件下载
- 下一篇: php从服务器下载文件