最模板 - 外贸网站建设,外贸网站模板

最模板

当前位置: 首页 > 建站教程 > php教程 >

php 文件下载方法

时间:2014-06-09 16:39来源: 作者: 点击: 次
文章也很简单我们只要用利用head发送头部信息就能实现把文件下载到本地了,有直接发送header信息也有把文件保存在服务器上再用header来发送哦,有需要的朋友参考下。 用的是表头方式下载,代

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