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.