PHP Download file

Source: Internet
Author: User
Tags php download

<?PHP/** * Download File * Header function **/Dl_file ($_get[' filename ']);functionDl_file ($file){ $file= ".//images//".$file; //First , see if the file exists  if(!Is_file($file )) {   die("<b>404 File not Found!</b>" ); }  //Gather relevent info about file $len=filesize($file ); $filename=basename($file ); $file _extension=Strtolower(substr(STRRCHR($filename, "."), 1 ) ); //This would set the Content-type to the appropriate setting for the file 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 is used for".)$file _extension. "Files!</b>" );  Break; default:$ctype= "Application/force-download"; } $file _temp=fopen($file, "R" ); //Begin Writing Headers Header("Pragma:public" ); Header("expires:0" ); Header("Cache-control:must-revalidate, post-check=0, pre-check=0" ); Header("Cache-control:public" ); Header("Content-description:file Transfer" ); //Use the switch-generated content-type Header("Content-type:$ctype" ); //Force the download $header= "content-disposition:attachment; Filename= ".$filename. ";"; Header($header ); Header("Content-transfer-encoding:binary" ); Header("Content-length:".)$len ); //@readfile ($file); Echo fread($file _temp,filesize($file ) ); fclose($file _temp ); Exit ();}?>

PHP Download file

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.