牛骨文教育服务平台(让学习变的简单)
博文笔记

web服务器如何判断客户端文件已下载结束?(php实现)

创建时间:2009-01-23 投稿人: 浏览次数:2508

对于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("下载失败");*/
?>

声明:该文观点仅代表作者本人,牛骨文系教育信息发布平台,牛骨文仅提供信息存储空间服务。