Php forces users to download by adding an http response header
In php, you can set the content-type of the header to force the user to download the content, rather than directly open the content in the browser. the following code is implemented: DownloadFile. php
- $ Filename = $ _ GET ['file']; // Get the fileid from the URL
- // Query the file ID
- $ Query = sprintf ("SELECT * FROM tableName WHERE id = '% s'", mysql_real_escape_string ($ filename ));
- $ SQL = mysql_query ($ query );
- If (mysql_num_rows ($ SQL)> 0 ){
- $ Row = mysql_fetch_array ($ SQL );
- // Set some headers
- Header ("Pragma: public ");
- Header ("Expires: 0 ");
- Header ("Cache-Control: must-revalidate, post-check = 0, pre-check = 0 ");
- Header ("Content-Type: application/force-download ");
- Header ("Content-Type: application/octet-stream ");
- Header ("Content-Type: application/download ");
- Header ("Content-Disposition: attachment; filename =". basename ($ row ['filename']). ";");
- Header ("Content-Transfer-Encoding: binary ");
- Header ("Content-Length:". filesize ($ row ['filename']);
- @ Readfile ($ row ['filename']);
- Exit (0 );
- } Else {
- Header ("Location :/");
- Exit;
- }
- ?>
Files. php
- Download
|
Php, http