PHP File Download class

Source: Internet
Author: User
Tags gtar

Copy codeThe Code is as follows: <?
// ================================================ ====================
// Example:
// $ Download = new download ('php, exe, html', false );
// If (! $ Download-> downloadfile ($ filename ))
//{
// Echo $ download-> geterrormsg ();
//}
// ================================================ ====================
Class download {
Var $ debug = true;
Var $ errormsg = '';
Var $ Filter = array ();
Var $ filename = '';
Var $ mineType = 'text/plain ';
Var $ xlq_filetype = array ();

Function download ($ fileFilter = '', $ isdebug = true)
{
$ This-> setFilter ($ fileFilter );
$ This-> setdebug ($ isdebug );
$ This-> setfiletype ();
}

Function setFilter ($ fileFilter)
{
If (empty ($ fileFilter) return;
$ This-> Filter = explode (',', strtolower ($ fileFilter ));
}
Function setdebug ($ debug)
{
$ This-> debug = $ debug;
}

Function setfilename ($ filename)
{
$ This-> filename = $ filename;
}

Function downloadfile ($ filename)
{
$ This-> setfilename ($ filename );
If ($ this-> filecheck ())
{
$ Fn = array_pop (explode ('/', strtr ($ this-> filename ,'\\','/')));
Header ("Pragma: public ");
Header ("Expires: 0"); // set expiration time
Header ("Cache-Component: must-revalidate, post-check = 0, pre-check = 0 ");
Header ("Content-type:". $ this-> mineType );
Header ("Content-Length:". filesize ($ this-> filename ));
Header ("Content-Disposition: attachment; filename = \" $ fn \"");
Header ('content-Transfer-Encoding: binary ');
Readfile ($ this-> filename );
Return true;
} Else
{
Return false;
}
}
Function geterrormsg ()
{
Return $ this-> errormsg;
}

Function filecheck ()
{
$ Filename = $ this-> filename;
If (file_exists ($ filename ))
{
$ Filetype = strtolower (array_pop (explode ('.', $ filename )));
If (in_array ($ filetype, $ this-> Filter ))
{
$ This-> errormsg. = $ filename. 'cannot be downloaded! ';
If ($ this-> debug) exit ($ filename. 'cannot be downloaded! ');
Return false;
} Else
{
If (function_exists ("mime_content_type "))
{
$ This-> mineType = mime_content_type ($ filename );
}
If (empty ($ this-> mineType ))
{
If (isset ($ this-> xlq_filetype [$ filetype]) $ this-> mineType = $ this-> xlq_filetype [$ filetype];
}
If (! Empty ($ this-> mineType ))
Return true;
Else
{
$ This-> errormsg. = 'get '. $ filename.' file type error occurs, or the specified file type does not exist ';
If ($ this-> debug) exit ('file type acquisition error ');
Return false;
}
}
} Else
{
$ This-> errormsg. = $ filename. 'does not exist! ';
If ($ this-> debug) exit ($ filename. 'does not exist! ');
Return false;
}
}

Function setfiletype ()
{
$ This-> xlq_filetype ['chm'] = 'application/octet-stream ';
$ This-> xlq_filetype ['ppt '] = 'application/vnd. ms-powerpoint ';
$ This-> xlq_filetype ['xls '] = 'application/vnd. ms-excel ';
$ This-> xlq_filetype ['Doc'] = 'application/msword ';
$ This-> xlq_filetype ['exe '] = 'application/octet-stream ';
$ This-> xlq_filetype ['rar '] = 'application/octet-stream ';
$ This-> xlq_filetype ['js'] = "javascript/js ";
$ This-> xlq_filetype ['css '] = "text/css ";
$ This-> xlq_filetype ['hqx'] = "application/mac-binhex40 ";
$ This-> xlq_filetype ['bin'] = "application/octet-stream ";
$ This-> xlq_filetype ['oda '] = "application/oda ";
$ This-> xlq_filetype ['pdf '] = "application/pdf ";
$ This-> xlq_filetype ['ai'] = "application/postsrcipt ";
$ This-> xlq_filetype ['eps '] = "application/postsrcipt ";
$ This-> xlq_filetype ['els'] = "application/postsrcipt ";
$ This-> xlq_filetype ['rtf '] = "application/rtf ";
$ This-> xlq_filetype ['miff'] = "application/x-mif ";
$ This-> xlq_filetype ['csh'] = "application/x-csh ";
$ This-> xlq_filetype ['dvi '] = "application/x-dvi ";
$ This-> xlq_filetype ['hdf'] = "application/x-hdf ";
$ This-> xlq_filetype ['nc '] = "application/x-netcdf ";
$ This-> xlq_filetype ['cdf'] = "application/x-netcdf ";
$ This-> xlq_filetype ['latex '] = "application/x-latex ";
$ This-> xlq_filetype ['ts'] = "application/x-troll-ts ";
$ This-> xlq_filetype ['src'] = "application/x-wais-source ";
$ This-> xlq_filetype ['zip'] = "application/zip ";
$ This-> xlq_filetype ['bcpio '] = "application/x-bcpio ";
$ This-> xlq_filetype ['cpio'] = "application/x-cpio ";
$ This-> xlq_filetype ['gtar'] = "application/x-gtar ";
$ This-> xlq_filetype ['shar '] = "application/x-shar ";
$ This-> xlq_filetype ['sv4cpio '] = "application/x-sv4cpio ";
$ This-> xlq_filetype ['sv4crc '] = "application/x-sv4crc ";
$ This-> xlq_filetype ['tar '] = "application/x-tar ";
$ This-> xlq_filetype ['ustar'] = "application/x-ustar ";
$ This-> xlq_filetype ['man'] = "application/x-troff-man ";
$ This-> xlq_filetype ['sh'] = "application/x-sh ";
$ This-> xlq_filetype ['tcl '] = "application/x-tcl ";
$ This-> xlq_filetype ['tex '] = "application/x-tex ";
$ This-> xlq_filetype ['text'] = "application/x-texinfo ";
$ This-> xlq_filetype ['textinfo'] = "application/x-texinfo ";
$ This-> xlq_filetype ['T'] = "application/x-troff ";
$ This-> xlq_filetype ['tr'] = "application/x-troff ";
$ This-> xlq_filetype ['roff'] = "application/x-troff ";
$ This-> xlq_filetype ['shar '] = "application/x-shar ";
$ This-> xlq_filetype ['me'] = "application/x-troll-me ";
$ This-> xlq_filetype ['ts'] = "application/x-troll-ts ";
$ This-> xlq_filetype ['gif'] = "image/gif ";
$ This-> xlq_filetype ['jpeg '] = "image/pjpeg ";
$ This-> xlq_filetype ['jpg '] = "image/pjpeg ";
$ This-> xlq_filetype ['jpe'] = "image/pjpeg ";
$ This-> xlq_filetype ['Ras '] = "image/x-cmu-raster ";
$ This-> xlq_filetype ['pbm'] = "image/x-portable-bitmap ";
$ This-> xlq_filetype ['ppm '] = "image/x-portable-pixmap ";
$ This-> xlq_filetype ['xbm '] = "image/x-xbitmap ";
$ This-> xlq_filetype ['xwd '] = "image/x-xwindowdump ";
$ This-> xlq_filetype ['ief'] = "image/ief ";
$ This-> xlq_filetype ['tif '] = "image/tiff ";
$ This-> xlq_filetype ['tiff '] = "image/tiff ";
$ This-> xlq_filetype ['pnm '] = "image/x-portable-anymap ";
$ This-> xlq_filetype ['pgm '] = "image/x-portable-graymap ";
$ This-> xlq_filetype ['rgb '] = "image/x-rgb ";
$ This-> xlq_filetype ['xpm '] = "image/x-xpixmap ";
$ This-> xlq_filetype ['txt '] = "text/plain ";
$ This-> xlq_filetype ['C'] = "text/plain ";
$ This-> xlq_filetype ['cc'] = "text/plain ";
$ This-> xlq_filetype ['H'] = "text/plain ";
$ This-> xlq_filetype ['html'] = "text/html ";
$ This-> xlq_filetype ['htm'] = "text/html ";
$ This-> xlq_filetype ['htl'] = "text/html ";
$ This-> xlq_filetype ['rtx'] = "text/richtext ";
$ This-> xlq_filetype ['etx'] = "text/x-setext ";
$ This-> xlq_filetype ['tsv'] = "text/tab-separated-values ";
$ This-> xlq_filetype ['mpeg '] = "video/mpeg ";
$ This-> xlq_filetype ['mpg'] = "video/mpeg ";
$ This-> xlq_filetype ['mpe'] = "video/mpeg ";
$ This-> xlq_filetype ['avi'] = "video/x-msvideo ";
$ This-> xlq_filetype ['qt '] = "video/quicktime ";
$ This-> xlq_filetype ['mov'] = "video/quicktime ";
$ This-> xlq_filetype ['moov '] = "video/quicktime ";
$ This-> xlq_filetype ['movi'] = "video/x-sgi-movie ";
$ This-> xlq_filetype ['au '] = "audio/basic ";
$ This-> xlq_filetype ['snd'] = "audio/basic ";
$ This-> xlq_filetype ['wav '] = "audio/x-wav ";
$ This-> xlq_filetype ['aif'] = "audio/x-aiff ";
$ This-> xlq_filetype ['aiff '] = "audio/x-aiff ";
$ This-> xlq_filetype ['aifc '] = "audio/x-aiff ";
$ This-> xlq_filetype ['swf '] = "application/x-shockwave-flash ";
}
}
?>

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.