PHP File Download

Source: Internet
Author: User

<?PHPHeader("content-type:text/html; Charset=utf-8 "); functionDownload$file _path){        $file _path=Iconv("Utf-8", "gb2312",$file _path); if(!Is_file($file _path)){             die("<b>file not found!</b>"); }                if(!file_exists($file _path)){             die("<b>file not found!</b>"); }                $dir _name=dirname($file _path); $file _name=basename($file _path); $len=filesize($file _path); $file _extension=Strtolower(substr(STRRCHR($file _name, "."), 1);//file suffix//According to the file suffix Judge Content-type        Switch($file _extension)        {             Case"PDF":$ctype= "Application/pdf";  Break;  Case"EXE":$ctype= "Application/octet-stream";  Break;  Case"Zip":$ctype= "Application/zip";  Break;  Case"Doc":$ctype= "Application/msword";  Break;  Case"XLS":$ctype= "Application/vnd.ms-excel";  Break;  Case"ppt":$ctype= "Application/vnd.ms-powerpoint";  Break;  Case"GIF":$ctype= "Image/gif";  Break;  Case"PNG":$ctype= "Image/png";  Break;  Case"JPEG": Case"JPG":$ctype= "Image/jpg";  Break;  Case"MP3":$ctype= "Audio/mpeg";  Break;  Case"WAV":$ctype= "Audio/x-wav";  Break;  Case"MPEG": Case"MPG": Case"MPE":$ctype= "Video/mpeg";  Break;  Case"MOV":$ctype= "Video/quicktime";  Break;  Case"Avi":$ctype= "Video/x-msvideo";  Break; //The following is for extensions that shouldn ' t is downloaded//(sensitive stuff, like PHP files)             Case"PHP": Case"HTM": Case"HTML": Case"TXT": die("<b>cannot download".$file _extension. "Files!</b>" );  Break; default:$ctype= "Application/force-download"; }                //Cache Control        Header("Cache-control:public" ); Header("Cache-control:must-revalidate, post-check=0, pre-check=0" ); Header("Pragma:no-cache" ); Header("expires:0" ); Header("Content-description:file Transfer" ); //Set the download file type and file name        Header("Content-type:".)$ctype); Header("Accept-ranges:bytes"); Header("Content-disposition:attachment; Filename= ".$file _name); Header("Content-transfer-encoding:binary" ); Header("Content-length:".)$len ); //read the file and write it to the client        $resource=fopen($file _path, "RB"); $block _size= 1024; $count= 0;  while(!feof($resource) &&$count<$len){            Echo fread($resource,$block _size); $count+=$block _size; }        fclose($resource); }        //=============================begin process==================================        $dir= "d:\\"; if(!Is_dir($dir)){         die("Dir not right!"); }        $dir=RTrim(Str_replace("\\","/",$dir),"/")." /"; //Accept Request Parameters    if(!Empty($_get) &&isset($_get["File"])){        $file=$_get["File"]; }        if(isset($file)){//If there is a request parameter, downloadDownload$dir.$file); }Else{//print out the files under the corresponding folder (no recursion, no folders)        $dir=Iconv("Utf-8", "gb2312",$dir); $files=Array(); if(Is_dir($dir)) {            if($DH=Opendir($dir)) {                 while(($file=Readdir($DH)) !==false) {                    if(Is_file($dir.$file)){                        Array_push($files,$file); }                }                Closedir($DH); }        }                if(Empty($files)){            Echo"<b>no File in Directory$dir</b> "; }Else{            foreach($files  as $one){            $one=Iconv("gb2312", "Utf-8",$one); Echo"<p>$one<a href= ' download.php?file=$one' > Click to download </a></p> '; }        }    }?>

PHP File Download

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.