A good php file download class
/**
- ** Php file download class
- ** Example:
- $ Download = new download ('php, exe, html', false );
- If (! $ Download-> downloadfile ($ filename ))
- {
- Echo $ download-> geterrormsg ();
- }
- * By bbs.it-home.org
- */
- 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 ";
- }
- }
- ?>
|