国内最全IT社区平台 联系我们 | 收藏本站
华晨云阿里云优惠2
您当前位置:首页 > php开源 > php教程 > php 文件下载方法

php 文件下载方法

来源:程序员人生   发布时间:2013-10-24 19:41:17 阅读次数:3055次

文章也很简单我们只要用利用head发送头部信息就能实现把文件下载到本地了,有直接发送header信息也有把文件保存在服务器上再用header来发送哦,有需要的朋友参考下。

用的是表头方式下载,代码如下:

  1. Header( "Content-type:application/octet-stream ");  
  2. Header( "Accept-Ranges:bytes ");  
  3. Header( "Accept-Length:".filesize($file_dir.$file_name));  
  4. Header( "Content-Disposition:attachment;filename= ".$file_name); 

判断文件存在再下载代码如下:

  1. <?php 
  2.     if( isset( $_REQUEST["download"] ) ) 
  3.     { 
  4.             $tfile = $_REQUEST["download"]; 
  5.              
  6.             if (file_exists($tfile)) 
  7.             { 
  8.                 $downfilename=substr($tfile,strrpos($tfile"/")+1); 
  9.                //文件保存的名字可以修改为你需要的,可以和链接给的名字不一样 
  10.                 // Prompt the user to download the new torrent file. 
  11.                 header( "Content-type: application/octet-streamn" ); 
  12.                 header( "Content-disposition: attachment; filename=$downfilenamen" ); 
  13.                 header( "Content-transfer-encoding: binaryn"); 
  14.                 header( "Content-length: " . @filesize($tfile ) . "n" ); 
  15.                 // Send the torrent file 
  16.                 $fp = @fopen$tfile"r" ); 
  17.                 @fpassthru$fp ); 
  18.                 @fclose( $fp ); 
  19.              } 
  20.              
  21.             exit(); 
  22.      } 
  23. ?> 

pdf文件下载,代码如下:

  1. <?php  
  2. $filename = $_SERVER['DOCUMENT_ROOT'] . "/path/to/file/my_file.pdf";  
  3. header("Cache-Control: public");  
  4. header("Content-Description: File Transfer");  
  5. header('Content-disposition: attachment; filename='.basename($filename));  
  6. header("Content-Type: application/pdf"); //pdf格式的 
  7. ?> 

下面讲讲header中的Content-type,不同的下载文件,对应不同的content-type,下面是大全:

  1. 'ez' => 'application/andrew-inset'
  2. 'hqx' => 'application/mac-binhex40'
  3. 'cpt' => 'application/mac-compactpro'
  4. 'doc' => 'application/msword'
  5. 'bin' => 'application/octet-stream'
  6. 'dms' => 'application/octet-stream'
  7. 'lha' => 'application/octet-stream',  
  8. 'lzh' => 'application/octet-stream'
  9. 'exe' => 'application/octet-stream'
  10. 'class' => 'application/octet-stream'
  11. 'so' => 'application/octet-stream'
  12. 'dll' => 'application/octet-stream'
  13. 'oda' => 'application/oda'
  14. 'pdf' => 'application/pdf'
  15. 'ai' => 'application/postscript'
  16. 'eps' => 'application/postscript'
  17. 'ps' => 'application/postscript'
  18. 'smi' => 'application/smil'
  19. 'smil' => 'application/smil'
  20. 'mif' => 'application/vnd.mif'
  21. 'xls' => 'application/vnd.ms-excel'
  22. 'ppt' => 'application/vnd.ms-powerpoint'
  23. 'wbxml' => 'application/vnd.wap.wbxml'
  24. 'wmlc' => 'application/vnd.wap.wmlc'
  25. 'wmlsc' => 'application/vnd.wap.wmlscriptc'
  26. 'bcpio' => 'application/x-bcpio'
  27. 'vcd' => 'application/x-cdlink'
  28. 'pgn' => 'application/x-chess-pgn'
  29. 'cpio' => 'application/x-cpio'
  30. 'csh' => 'application/x-csh',  
  31. 'dcr' => 'application/x-director'
  32. 'dir' => 'application/x-director'
  33. 'dxr' => 'application/x-director'
  34. 'dvi' => 'application/x-dvi'
  35. 'spl' => 'application/x-futuresplash'
  36. 'gtar' => 'application/x-gtar'
  37. 'hdf' => 'application/x-hdf'
  38. 'js' => 'application/x-javascript'
  39. 'skp' => 'application/x-koan'
  40. 'skd' => 'application/x-koan'
  41. 'skt' => 'application/x-koan'
  42. 'skm' => 'application/x-koan'
  43. 'latex' => 'application/x-latex'
  44. 'nc' => 'application/x-netcdf'
  45. 'cdf' => 'application/x-netcdf'
  46. 'sh' => 'application/x-sh'
  47. 'shar' => 'application/x-shar'
  48. 'swf' => 'application/x-shockwave-flash'
  49. 'sit' => 'application/x-stuffit'
  50. 'sv4cpio' => 'application/x-sv4cpio'
  51. 'sv4crc' => 'application/x-sv4crc'
  52. 'tar' => 'application/x-tar'
  53. 'tcl' => 'application/x-tcl'
  54. 'tex' => 'application/x-tex',  
  55. 'texinfo' => 'application/x-texinfo'
  56. 'texi' => 'application/x-texinfo'
  57. 't' => 'application/x-troff'
  58. 'tr' => 'application/x-troff'
  59. 'roff' => 'application/x-troff'
  60. 'man' => 'application/x-troff-man'
  61. 'me' => 'application/x-troff-me'
  62. 'ms' => 'application/x-troff-ms'
  63. 'ustar' => 'application/x-ustar'
  64. 'src' => 'application/x-wais-source'
  65. 'xhtml' => 'application/xhtml+xml'
  66. 'xht' => 'application/xhtml+xml',  
  67. 'zip' => 'application/zip'
  68. 'au' => 'audio/basic'
  69. 'snd' => 'audio/basic'
  70. 'mid' => 'audio/midi'
  71. 'midi' => 'audio/midi'
  72. 'kar' => 'audio/midi'
  73. 'mpga' => 'audio/mpeg'
  74. 'mp2' => 'audio/mpeg'
  75. 'mp3' => 'audio/mpeg'
  76. 'aif' => 'audio/x-aiff'
  77. 'aiff' => 'audio/x-aiff'
  78. 'aifc' => 'audio/x-aiff'
  79. 'm3u' => 'audio/x-mpegurl'
  80. 'ram' => 'audio/x-pn-realaudio'
  81. 'rm' => 'audio/x-pn-realaudio'
  82. 'rpm' => 'audio/x-pn-realaudio-plugin'
  83. 'ra' => 'audio/x-realaudio'
  84. 'wav' => 'audio/x-wav'
  85. 'pdb' => 'chemical/x-pdb'
  86. 'xyz' => 'chemical/x-xyz'
  87. 'bmp' => 'image/bmp'
  88. 'gif' => 'image/gif'
  89. 'ief' => 'image/ief'
  90. 'jpeg' => 'image/jpeg'
  91. 'jpg' => 'image/jpeg'
  92. 'jpe' => 'image/jpeg',  
  93. 'png' => 'image/png'
  94. 'tiff' => 'image/tiff'
  95. 'tif' => 'image/tiff'
  96. 'djvu' => 'image/vnd.djvu'
  97. 'djv' => 'image/vnd.djvu'
  98. 'wbmp' => 'image/vnd.wap.wbmp'
  99. 'ras' => 'image/x-cmu-raster'
  100. 'pnm' => 'image/x-portable-anymap'
  101. 'pbm' => 'image/x-portable-bitmap'
  102. 'pgm' => 'image/x-portable-graymap'
  103. 'ppm' => 'image/x-portable-pixmap'
  104. 'rgb' => 'image/x-rgb',  
  105. 'xbm' => 'image/x-xbitmap'
  106. 'xpm' => 'image/x-xpixmap'
  107. 'xwd' => 'image/x-xwindowdump'
  108. 'igs' => 'model/iges'
  109. 'iges' => 'model/iges'
  110. 'msh' => 'model/mesh'
  111. 'mesh' => 'model/mesh'
  112. 'silo' => 'model/mesh'
  113. 'wrl' => 'model/vrml'
  114. 'vrml' => 'model/vrml'
  115. 'css' => 'text/css'
  116. 'html' => 'text/html'
  117. 'htm' => 'text/html',  
  118. 'asc' => 'text/plain'
  119. 'txt' => 'text/plain'
  120. 'rtx' => 'text/richtext'
  121. 'rtf' => 'text/rtf'
  122. 'sgml' => 'text/sgml'
  123. 'sgm' => 'text/sgml'
  124. 'tsv' => 'text/tab-separated-values'
  125. 'wml' => 'text/vnd.wap.wml'
  126. 'wmls' => 'text/vnd.wap.wmlscript'
  127. 'etx' => 'text/x-setext'
  128. 'xsl' => 'text/xml'
  129. 'xml' => 'text/xml'
  130. 'mpeg' => 'video/mpeg'
  131. 'mpg' => 'video/mpeg'
  132. 'mpe' => 'video/mpeg'
  133. 'qt' => 'video/quicktime'
  134. 'mov' => 'video/quicktime'
  135. 'mxu' => 'video/vnd.mpegurl'
  136. 'avi' => 'video/x-msvideo'
  137. 'movie' => 'video/x-sgi-movie'
  138. 'ice' => 'x-conference/x-cooltalk'
生活不易,码农辛苦
如果您觉得本网站对您的学习有所帮助,可以手机扫描二维码进行捐赠
程序员人生
------分隔线----------------------------
分享到:
------分隔线----------------------------
关闭
程序员人生