Php method for downloading various files based on the header function

Source: Internet
Author: User
This article mainly for you in detail the PHP using the header function to download various files of the code, interested in the small partners can refer to

Specific content is as follows

<?php/*** Download File * Header function **/dl_file ($_get [' filename ']), function Dl_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": $ctyp   E = "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 ();}? >

Summary: The above is the entire content of this article, I hope to be able to help you learn.

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.