Php code for downloading images

Source: Internet
Author: User
Tags gtar
This allows you to download images as attachments. Please refer to the following program! This allows you to download images as attachments. Please refer to the following program!

// File directory define ('dl _ dir', 'temp /'); // MIME type corresponding to common extensions $ MIMETypes = array ('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/postscr reject pt ', 'eps '=> 'application/postscr limit pt', 'Ps' => 'application/postscr limit pt', 'smi' => 'application/smil ', 'smi' => 'application/smi', 'MI' => 'application/vnd. mif ', 'XLS' => 'application/vnd. ms-excel ', 'Ppt '=> 'application/vnd. ms-powerpoint ', 'wbxml' => 'application/vnd. wap. wbxml', 'wmlc' => 'application/vnd. wap. wmlc ', 'wmlsc' => 'application/vnd. wap. wmlscr extends ptc ', '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-Forwarded Cr limit pt', 'skp' => 'application/x-koan ', 'skd' => 'application/x-koanc', 'skt' => 'application/x-koanc', '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-texinfo', 'text' => 'application/x-texinfo ','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' => 'A Udio/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 ', 'iges' => 'model/iges ', 'msh' => 'model/mesh ', 'mesh' => 'model/mesh ', 'silo' => 'model/mesh ', 'wrl' => 'model/XML', 'xml' => 'Model/XML', 'css '=> 'text/css', 'html' => 'text/html', 'htm' => 'text/html ', 'asc '=> 'text/plain', 'txt '=> 'text/plain', 'rtx' => 'text/richtext ', 'text' => 'text/rtf ', 'sgml' => 'text/sgml', 'sgm' => 'text/sgml ', 'tsv' => 'text/tab-separated-values', 'wml' => 'text/vnd. wap. wml', 'wmls' => 'text/vnd. wap. wmlscr limit pt ', '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-movie ', 'Ice '=> 'X-conference/x-cooltalk',);?>

The code is as follows:

 Function Reference-> Directories node) Description: In windows, the backslash '\' is used, in Linux, '/' is used, and in windows, both of them are used normally, but linux does not know, it looks professional for the sake of standards. */$ filePath = str_replace (array ('/', '\'), DIRECTORY_SEPARATOR, $ filePath); if (! Is_file ($ filePath) {exit ('File: '. $ fileName.' not exists! ');} Else {// if it is a valid file // File extension $ fileExt = substr (strrchr ($ fileName ,'. '), 1); // File Type $ fileType = $ MIMETypes [$ fileExt]? $ MIMETypes [$ fileExt]: 'application/octet-stream'; // whether the image is $ isImage = False;/* Description: getimagesize (). For details, see the manual description: an effective method for determining whether a file is an image. it is often used for file Upload verification */$ imgInfo = @ getimagesize ($ filePath ); if ($ imgInfo [2] & $ imgInfo ['bits ']) {$ isImage = True;} // display method $ attachment = $ isImage? 'Inline': 'attachment'; // read the file if (is_readable ($ filePath) {/* Description: ob_end_clean () clears and closes the output buffer. for details, see the manual description: disable the output buffer so that the file fragment content is sent out after being read to the memory, reducing resource consumption */ob_end_clean ();/* HTTP header information: indicates that the client can receive responses with a lifetime not greater than the specified time (in seconds) */header ('cache-control: max-age = 31536000 ');/* HTTP header information: cache file Expiration Time (Greenwich Mean Time) */header ('expires :'. gmdate ('d, d m y h: I: S', time () + 31536000 ). 'gmt');/* HTTP header information: The last modification time of the file during the service period: Cache-control, Expires, L Ast-Modified controls the browser's cached header information in some portals with a large volume of traffic. reasonable cache settings can avoid excessive server requests, to some extent, relieve the pressure on the server */header ('last-Modified :'. gmdate ('d, d m y h: I: S', filemtime ($ filePath ). 'gmt');/* HTTP header information: The file encoding (Encode) method. because the file requested by the attachment is diverse, changing the encoding method may damage the file, it is none */header ('content-Encoding: none');/* HTTP header information: indicates the file type currently requested by the browser. 1. if it is always specified as application/octet-stream, it indicates that the file is a binary stream and the download is always prompted. 2. specify the corresponding type. if the requested mp3 file and the corresponding MIME type is audio/mpeg, IE will automatically start Windows Media Player. */header ('content-type :'. $ fileType);/* HTTP header information: if it is attachment, the "file download" dialog box is displayed on the browser, and specify the default name of the file to be saved (which can be different from the file name on the server). If you want the browser to directly display the content, you must specify the name as inline and slice, text */header ('content-Disposition :'. $ attachment. '; filename = '. $ fileName);/* HTTP header information: tell the browser the file length (does the file size information exist when the IE file is downloaded ?) */Header ('content-Length :'. filesize ($ filePath); // open the file (binary read-only mode) $ fp = fopen ($ filePath, 'RB'); // output file fpassthru ($ fp ); // close the file fclose ($ fp); exit () ;}else {exit ('read file failed! ') ;}}?>

The above is the php code _ php tips for downloading images. For more information, see The PHP Chinese website (www.php1.cn )!

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.