PHP 实现文件下载

$dir = 'article.xml'; //存放文件的路径
if(file_exists($dir)){
  $file = fopen ($dir,"r");
  //输入文件标签
  Header ( "Content-type: application/octet-stream" );
  Header ( "Accept-Ranges: bytes" );
  Header ( "Accept-Length: " . filesize ($dir) );
  Header ( "Content-Disposition: attachment; filename=article.xml");
  echo fread ( $file, filesize ($dir) );
  fclose ( $file );
  exit ();
}else{
  header('HTTP/1.1 404 Not Found');
}

文件<a href = "http://localhost/down.zip">下载</a> 添加a链接是可以的

网上说的heaer头跳转有时会不好用,此方法对于小文件亲测可用。

大文件下载和数据流方式下载的方法待续。。。