PHP file download method _ PHP Tutorial

Source: Internet
Author: User
Tags gtar
Php file download method. The article is also very simple. we only need to use the head to send the header information to download the file to the local file, directly sending header information, saving the file on the server, and then sending the article with the header is also very simple. we only need to use the head to send the header information to download the file to the local, you can either directly send the header information or store the file on the server and then use the header to send it. For more information, see.

Download using the header method

The code is as follows:
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 );

Determine whether the file exists before downloading

The code is as follows:
If (isset ($ _ REQUEST ["download"])
{
$ Tfile = $ _ REQUEST ["download"];

If (file_exists ($ tfile ))
{
$ Downfilename = substr ($ tfile, strrpos ($ tfile, "/") + 1 );
// The name stored in the file can be changed to what you need. it can be different from the name given by the link.
// 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 ();
}
?>

PDF file download

The code is as follows:

$ Filename = $ _ SERVER ['document _ root']. "/path/to/file/my_file.pdf ";
Header ("Cache-Control: public ");
Header ("Content-Description: File Transfer ");
Header ('content-disposition: attachment; filename = '. basename ($ filename ));
Header ("Content-Type: application/pdf"); // pdf format

?>


The Content-type in the header is described as follows:
Different download files correspond to different content-types. The following is a complete example:
PHP code
{
'EZ '=> 'application/andrew-inset ',
'Hqx' => 'application/mac-binhex40 ',
'Cpt' => 'application/mac-compactpro ',
'Doc' => 'application/msword ',
'Bin' => 'application/octet-stream ',
'Dms '=> 'application/octet-stream ',
'Lha' => 'application/octet-stream ',
'Lzh '=> 'application/octet-stream ',
'Exe '=> 'application/octet-stream ',
'Class' => 'application/octet-stream ',
'So' => 'application/octet-stream ',
'Dll '=> 'application/octet-stream ',
'Oda '=> 'application/oda ',
'PDF '=> 'application/pdf ',
'AI' => 'application/postscript ',
'Eps '=> 'application/postscript ',
'PS '=> 'application/postscript ',
'Smi' => 'application/smil ',
'Smil '=> 'application/smil ',
'Mfa' => 'application/vnd. mfa ',
'XLS '=> 'application/vnd. ms-excel ',
'Ppt '=> 'application/vnd. ms-powerpoint ',
'Wbxml' => 'application/vnd. wap. wbxml ',
'Wmlc' => 'application/vnd. wap. wmlc ',
'Wmlsc '=> 'application/vnd. wap. wmlscriptc ',
'Bcpio' => 'application/x-bcpio ',
'Vcd' => 'application/x-cdlink ',
'Pgn' => 'application/x-chess-pgn ',
'Cpio' => 'application/x-cpio ',
'CSH' => 'application/x-csh ',
'Dcr '=> 'application/x-ctor ',
'Dir' => 'application/x-ctor ',
'Dxr' => 'application/x-ctor ',
'Dvi '=> 'application/x-dvi ',
'Spl' => 'application/x-futuresplash ',
'Gtar '=> 'application/x-gtar ',
'Hdf' => 'application/x-hdf ',
'Js' => 'application/x-javascript ',
'Skp' => 'application/x-koan ',
'Skd' => 'application/x-koan ',
'Skt' => 'application/x-koan ',
'Skm' => 'application/x-koan ',
'Latex '=> 'application/x-latex ',
'NC '=> 'application/x-netcdf ',
'Cdf' => 'application/x-netcdf ',
'Sh' => 'application/x-sh ',
'Shar '=> 'application/x-shar ',
'Swf '=> 'application/x-shockwave-Flash ',
'Sit '=> 'application/x-stuffit ',
'Sv4cpio '=> 'application/x-sv4cpio ',
'Sv4crc '=> 'application/x-sv4crc ',
'Tar '=> 'application/x-tar ',
'Tcl '=> 'application/x-tcl ',
'Tex '=> 'application/x-tex ',
'Textinfo' => 'application/x-textinfo ',
'Text' => 'application/x-textinfo ',
'T' => 'application/x-troff ',
'Tr' => 'application/x-troff ',
'Roff' => 'application/x-troff ',
'Man '=> 'application/x-troff-man ',
'Me' => 'application/x-troff-me ',
'Ms' => 'application/x-troff-MS ',
'Ustar' => 'application/x-ustar ',
'Src' => 'application/x-wais-source ',
'Xhtml '=> 'application/xhtml + XML ',
'Xht '=> 'application/xhtml + XML ',
'Zip' => 'application/zip ',
'Au '=> 'audio/basic ',
'Snd' => 'audio/basic ',
'Mid '=> 'audio/midi ',
'Midi '=> 'audio/midi ',
'Kar '=> 'audio/midi ',
'Mpga' => 'audio/mpeg ',
'Mp2' => 'audio/mpeg ',
'Mp3' => 'audio/mpeg ',
'Aif' => 'audio/x-aiff ',
'Aiff '=> 'audio/x-aiff ',
'Aifc '=> 'audio/x-aiff ',
'M3u' => 'audio/x-mpegurl ',
'Ram '=> 'audio/x-pn-realaudio ',
'Rm '=> 'audio/x-pn-realaudio ',
'Rpm '=> 'audio/x-pn-realaudio-plugin ',
'A' => 'audio/x-realaudio ',
'Wav '=> 'audio/x-wav ',
'Pdb' => 'chemical/x-pdb ',
'XYZ' => 'chemical/x-XYZ ',
'Bmp '=> 'image/bmp ',
'Gif' => 'image/GIF ',
'Ief' => 'image/ief ',
'Jpeg '=> 'image/jpeg ',
'Jpg '=> 'image/jpeg ',
'Jpe' => 'image/jpeg ',
'PNG '=> 'image/png ',
'Tiff '=> 'image/tiff ',
'Tif '=> 'image/tiff ',
'Djvu '=> 'image/vnd. djvu ',
'Djv' => 'image/vnd. djvu ',
'Wbmp '=> 'image/vnd. wap. wbmp ',
'Ras '=> 'image/x-cmu-raster ',
'Pnm '=> 'image/x-portable-anymap ',
'Pbm' => 'image/x-portable-bitmap ',
'Pgm '=> 'image/x-portable-graymap ',
'Ppm '=> 'image/x-portable-pixmap ',
'Rgb '=> 'image/x-rgb ',
'Xbm '=> 'image/x-xbitmap ',
'Xpm' => 'image/x-xpixmap ',
'Xwd '=> 'image/x-xwindowdump ',
'Gies' => 'model/iges ',
& Apos; iges & apos; = & apos; model/iges & apos ',
'Msh' => 'model/mesh ',
'Mesh '=> 'model/mesh ',
'Silo' => 'model/mesh ',
'Wrl' => 'model/jpa ',
'Jpa' => 'model/jpa ',
'Css '=> 'text/css ',
'Html' => 'text/html ',
'Htm '=> 'text/html ',
'Asc '=> 'text/plain ',
'Txt '=> 'text/plain ',
'Rtx' => 'text/richtext ',
'Rtf '=> 'text/rtf ',
'Sgml' => 'text/sgml ',
'Sgm' => 'text/sgml ',
'Tsv' => 'text/tab-separated-values ',
'Wml' => 'text/vnd. wap. wml ',
'Wmls' => 'text/vnd. wap. wmlscript ',
'Etx' => 'text/x-setext ',
'Xsl '=> 'text/XML ',
'Xml' => 'text/XML ',
'MPEG '=> 'video/mpeg ',
'Mpg' => 'video/mpeg ',
'Mpe' => 'video/mpeg ',
'Qt '=> 'video/quicktime ',
'Mov' => 'video/quicktime ',
'Mxu' => 'video/vnd. mpegurl ',
'Av' => 'video/x-msvideo ',
'Movi' => 'video/x-sgi-Movi ',
'Ice '=> 'X-conference/x-cooltalk ',
}

By sending the header information, the producer can download the file to the local file, directly send the header information, save the file on the server, and then use the header to send the file...

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.