Heim > php教程 > php手册 > php 文件下载方法

php 文件下载方法

WBOY
Freigeben: 2016-05-25 16:51:11
Original
978 Leute haben es durchsucht

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

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

Header( "Content-type:application/octet-stream ");  
Header( "Accept-Ranges:bytes ");  
Header( "Accept-Length:".filesize($file_dir.$file_name));  
Header( "Content-Disposition:attachment;filename= ".$file_name);
Nach dem Login kopieren

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

<?php
if (isset($_REQUEST["download"])) {
    $tfile = $_REQUEST["download"];
    if (file_exists($tfile)) {
        $downfilename = substr($tfile, strrpos($tfile, "/") + 1);
        //文件保存的名字可以修改为你需要的,可以和链接给的名字不一样
        // Prompt the user to download the new torrent file.
        header("Content-type: application/octet-streamn");
        header("Content-disposition: attachment; filename=$downfilenamen");
        header("Content-transfer-encoding: binaryn");
        header("Content-length: " . @filesize($tfile) . "n");
        // Send the torrent file
        $fp = @fopen($tfile, "r");
        @fpassthru($fp);
        @fclose($fp);
    }
    exit();
}
?>
Nach dem Login kopieren

pdf文件下载,代码如下:

<?php
$filename = $_SERVER[&#39;DOCUMENT_ROOT&#39;] . "/path/to/file/my_file.pdf";
header("Cache-Control: public");
header("Content-Description: File Transfer");
header(&#39;Content-disposition: attachment; filename=&#39; . basename($filename));
header("Content-Type: application/pdf"); //pdf格式的
?>
Nach dem Login kopieren

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

{ 
	&#39;ez&#39; => &#39;application/andrew-inset&#39;, 
	&#39;hqx&#39; => &#39;application/mac-binhex40&#39;, 
	&#39;cpt&#39; => &#39;application/mac-compactpro&#39;, 
	&#39;doc&#39; => &#39;application/msword&#39;, 
	&#39;bin&#39; => &#39;application/octet-stream&#39;, 
	&#39;dms&#39; => &#39;application/octet-stream&#39;, 
	&#39;lha&#39; => &#39;application/octet-stream&#39;,  
	&#39;lzh&#39; => &#39;application/octet-stream&#39;, 
	&#39;exe&#39; => &#39;application/octet-stream&#39;, 
	&#39;class&#39; => &#39;application/octet-stream&#39;, 
	&#39;so&#39; => &#39;application/octet-stream&#39;, 
	&#39;dll&#39; => &#39;application/octet-stream&#39;, 
	&#39;oda&#39; => &#39;application/oda&#39;, 
	&#39;pdf&#39; => &#39;application/pdf&#39;, 
	&#39;ai&#39; => &#39;application/postscript&#39;, 
	&#39;eps&#39; => &#39;application/postscript&#39;, 
	&#39;ps&#39; => &#39;application/postscript&#39;, 
	&#39;smi&#39; => &#39;application/smil&#39;, 
	&#39;smil&#39; => &#39;application/smil&#39;, 
	&#39;mif&#39; => &#39;application/vnd.mif&#39;, 
	&#39;xls&#39; => &#39;application/vnd.ms-excel&#39;, 
	&#39;ppt&#39; => &#39;application/vnd.ms-powerpoint&#39;, 
	&#39;wbxml&#39; => &#39;application/vnd.wap.wbxml&#39;, 
	&#39;wmlc&#39; => &#39;application/vnd.wap.wmlc&#39;, 
	&#39;wmlsc&#39; => &#39;application/vnd.wap.wmlscriptc&#39;, 
	&#39;bcpio&#39; => &#39;application/x-bcpio&#39;, 
	&#39;vcd&#39; => &#39;application/x-cdlink&#39;, 
	&#39;pgn&#39; => &#39;application/x-chess-pgn&#39;, 
	&#39;cpio&#39; => &#39;application/x-cpio&#39;, 
	&#39;csh&#39; => &#39;application/x-csh&#39;,  
	&#39;dcr&#39; => &#39;application/x-director&#39;, 
	&#39;dir&#39; => &#39;application/x-director&#39;, 
	&#39;dxr&#39; => &#39;application/x-director&#39;, 
	&#39;dvi&#39; => &#39;application/x-dvi&#39;, 
	&#39;spl&#39; => &#39;application/x-futuresplash&#39;, 
	&#39;gtar&#39; => &#39;application/x-gtar&#39;, 
	&#39;hdf&#39; => &#39;application/x-hdf&#39;, 
	&#39;js&#39; => &#39;application/x-javascript&#39;, 
	&#39;skp&#39; => &#39;application/x-koan&#39;, 
	&#39;skd&#39; => &#39;application/x-koan&#39;, 
	&#39;skt&#39; => &#39;application/x-koan&#39;, 
	&#39;skm&#39; => &#39;application/x-koan&#39;, 
	&#39;latex&#39; => &#39;application/x-latex&#39;, 
	&#39;nc&#39; => &#39;application/x-netcdf&#39;, 
	&#39;cdf&#39; => &#39;application/x-netcdf&#39;, 
	&#39;sh&#39; => &#39;application/x-sh&#39;, 
	&#39;shar&#39; => &#39;application/x-shar&#39;, 
	&#39;swf&#39; => &#39;application/x-shockwave-flash&#39;, 
	&#39;sit&#39; => &#39;application/x-stuffit&#39;, 
	&#39;sv4cpio&#39; => &#39;application/x-sv4cpio&#39;, 
	&#39;sv4crc&#39; => &#39;application/x-sv4crc&#39;, 
	&#39;tar&#39; => &#39;application/x-tar&#39;, 
	&#39;tcl&#39; => &#39;application/x-tcl&#39;, 
	&#39;tex&#39; => &#39;application/x-tex&#39;,  
	&#39;texinfo&#39; => &#39;application/x-texinfo&#39;, 
	&#39;texi&#39; => &#39;application/x-texinfo&#39;, 
	&#39;t&#39; => &#39;application/x-troff&#39;, 
	&#39;tr&#39; => &#39;application/x-troff&#39;, 
	&#39;roff&#39; => &#39;application/x-troff&#39;, 
	&#39;man&#39; => &#39;application/x-troff-man&#39;, 
	&#39;me&#39; => &#39;application/x-troff-me&#39;, 
	&#39;ms&#39; => &#39;application/x-troff-ms&#39;, 
	&#39;ustar&#39; => &#39;application/x-ustar&#39;, 
	&#39;src&#39; => &#39;application/x-wais-source&#39;, 
	&#39;xhtml&#39; => &#39;application/xhtml+xml&#39;, 
	&#39;xht&#39; => &#39;application/xhtml+xml&#39;,  
	&#39;zip&#39; => &#39;application/zip&#39;, 
	&#39;au&#39; => &#39;audio/basic&#39;, 
	&#39;snd&#39; => &#39;audio/basic&#39;, 
	&#39;mid&#39; => &#39;audio/midi&#39;, 
	&#39;midi&#39; => &#39;audio/midi&#39;, 
	&#39;kar&#39; => &#39;audio/midi&#39;, 
	&#39;mpga&#39; => &#39;audio/mpeg&#39;, 
	&#39;mp2&#39; => &#39;audio/mpeg&#39;, 
	&#39;mp3&#39; => &#39;audio/mpeg&#39;, 
	&#39;aif&#39; => &#39;audio/x-aiff&#39;, 
	&#39;aiff&#39; => &#39;audio/x-aiff&#39;, 
	&#39;aifc&#39; => &#39;audio/x-aiff&#39;, 
	&#39;m3u&#39; => &#39;audio/x-mpegurl&#39;, 
	&#39;ram&#39; => &#39;audio/x-pn-realaudio&#39;, 
	&#39;rm&#39; => &#39;audio/x-pn-realaudio&#39;, 
	&#39;rpm&#39; => &#39;audio/x-pn-realaudio-plugin&#39;, 
	&#39;ra&#39; => &#39;audio/x-realaudio&#39;, 
	&#39;wav&#39; => &#39;audio/x-wav&#39;, 
	&#39;pdb&#39; => &#39;chemical/x-pdb&#39;, 
	&#39;xyz&#39; => &#39;chemical/x-xyz&#39;, 
	&#39;bmp&#39; => &#39;image/bmp&#39;, 
	&#39;gif&#39; => &#39;image/gif&#39;, 
	&#39;ief&#39; => &#39;image/ief&#39;, 
	&#39;jpeg&#39; => &#39;image/jpeg&#39;, 
	&#39;jpg&#39; => &#39;image/jpeg&#39;, 
	&#39;jpe&#39; => &#39;image/jpeg&#39;,  
	&#39;png&#39; => &#39;image/png&#39;, 
	&#39;tiff&#39; => &#39;image/tiff&#39;, 
	&#39;tif&#39; => &#39;image/tiff&#39;, 
	&#39;djvu&#39; => &#39;image/vnd.djvu&#39;, 
	&#39;djv&#39; => &#39;image/vnd.djvu&#39;, 
	&#39;wbmp&#39; => &#39;image/vnd.wap.wbmp&#39;, 
	&#39;ras&#39; => &#39;image/x-cmu-raster&#39;, 
	&#39;pnm&#39; => &#39;image/x-portable-anymap&#39;, 
	&#39;pbm&#39; => &#39;image/x-portable-bitmap&#39;, 
	&#39;pgm&#39; => &#39;image/x-portable-graymap&#39;, 
	&#39;ppm&#39; => &#39;image/x-portable-pixmap&#39;, 
	&#39;rgb&#39; => &#39;image/x-rgb&#39;,  
	&#39;xbm&#39; => &#39;image/x-xbitmap&#39;, 
	&#39;xpm&#39; => &#39;image/x-xpixmap&#39;, 
	&#39;xwd&#39; => &#39;image/x-xwindowdump&#39;, 
	&#39;igs&#39; => &#39;model/iges&#39;, 
	&#39;iges&#39; => &#39;model/iges&#39;, 
	&#39;msh&#39; => &#39;model/mesh&#39;, 
	&#39;mesh&#39; => &#39;model/mesh&#39;, 
	&#39;silo&#39; => &#39;model/mesh&#39;, 
	&#39;wrl&#39; => &#39;model/vrml&#39;, 
	&#39;vrml&#39; => &#39;model/vrml&#39;, 
	&#39;css&#39; => &#39;text/css&#39;, 
	&#39;html&#39; => &#39;text/html&#39;, 
	&#39;htm&#39; => &#39;text/html&#39;,  
	&#39;asc&#39; => &#39;text/plain&#39;, 
	&#39;txt&#39; => &#39;text/plain&#39;, 
	&#39;rtx&#39; => &#39;text/richtext&#39;, 
	&#39;rtf&#39; => &#39;text/rtf&#39;, 
	&#39;sgml&#39; => &#39;text/sgml&#39;, 
	&#39;sgm&#39; => &#39;text/sgml&#39;, 
	&#39;tsv&#39; => &#39;text/tab-separated-values&#39;, 
	&#39;wml&#39; => &#39;text/vnd.wap.wml&#39;, 
	&#39;wmls&#39; => &#39;text/vnd.wap.wmlscript&#39;, 
	&#39;etx&#39; => &#39;text/x-setext&#39;, 
	&#39;xsl&#39; => &#39;text/xml&#39;, 
	&#39;xml&#39; => &#39;text/xml&#39;, 
	&#39;mpeg&#39; => &#39;video/mpeg&#39;, 
	&#39;mpg&#39; => &#39;video/mpeg&#39;, 
	&#39;mpe&#39; => &#39;video/mpeg&#39;, 
	&#39;qt&#39; => &#39;video/quicktime&#39;, 
	&#39;mov&#39; => &#39;video/quicktime&#39;, 
	&#39;mxu&#39; => &#39;video/vnd.mpegurl&#39;, 
	&#39;avi&#39; => &#39;video/x-msvideo&#39;, 
	&#39;movie&#39; => &#39;video/x-sgi-movie&#39;, 
	&#39;ice&#39; => &#39;x-conference/x-cooltalk&#39;, 
	}
Nach dem Login kopieren

               
               

本文地址:

转载随意,但请附上文章地址:-)

Verwandte Etiketten:
Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage