PHP file Download class _php tips

Source: Internet
Author: User
Tags explode zip gtar

Copy Code code as follows:
?
//====================================================
Use 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. ' Not allowed to download! ';
if ($this->debug) exit ($filename. ' Not allowed to download! ') ;
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 there is no predetermined file type ';
if ($this->debug) exit (' Get file type error ');
return false;
}
}
}else
{
$this->errormsg.= $filename. ' 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[' es ']= "Application/postsrcipt";
$this->xlq_filetype[' rtf ']= "Application/rtf";
$this->xlq_filetype[' MIF ']= "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[' Texi ']= "Application/x-texinfo";
$this->xlq_filetype[' texinfo ']= "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[' movie ']= "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.