Php code for Force File Download (ie browser)
- // Header ("Cache-Control: public ");
- Header ('content-type: application/vnd. ms-excel ');
- Header ("Content-Disposition: attachment; filenameappsreport.xls ");
If you do not add the first sentence, Internet Explorer cannot download **. php (from The ** website ). Internet Explorer cannot open this internet website. The requested website is unavailable or cannot be found. please try again later. In addition, the name is not the set name: report.xls, But **. php. you can add the first sentence. When reading rar, gif, and so on, the first sentence is not added. the error box is not displayed! For images such as gif, Content-Disposition: attachment; will force a save dialog box to pop up. If it is omitted or inline, it will be displayed directly on the webpage. The value of Content-type is as follows:
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 are for extensions that shouldn't be downloaded (sensitive stuff, like php files)
- Case "php ":
- Case "htm ":
- Case "html ":
- Case "txt": die ("Cannot be used for ". $ file_extension." files!"); Break;
Default: $ ctype = "application/force-download ";
- }
|