Download some files using php. This is generally required to hide the files. Otherwise, it will increase the burden on the server. It is better to provide the software address directly.
A simple PHP file downloads the source code. Although resumable data transfer is not supported, it can meet some common requirements. Php download file is actually implemented with a tag, such as <a href = "web/magento-1.8.1.0.zip"> magento-1.8.1.0.zip </a>. However, when some page views can be identified, such as. txt,. html, and other formats, you must use <a href = "web/abc.txt"> abc.txt </a> to know what will happen.
<? Php/*** File download ***/header ("Content-type: text/html; charset = UTF-8"); download ('web/magento-1.8.1.0.zip ', 'magento download'); function download ($ file, $ down_name) {$ suffix = substr ($ file, strrpos ($ file ,'. '); // get the file suffix $ down_name = $ down_name. $ suffix; // new file name, that is, the downloaded name // determine whether a given file exists or not if (! File_exists ($ file) {die ("the file you want to download does not exist, it may be deleted");} $ fp = fopen ($ file, "r "); $ file_size = filesize ($ file); // header ("Content-type: application/octet-stream") required for downloading files; header ("Accept-Ranges: bytes "); header (" Accept-Length :". $ file_size); header ("Content-Disposition: attachment; filename = ". $ down_name); $ buffer = 1024; $ file_count = 0; // return data to the browser while (! Feof ($ fp) & $ file_count <$ file_size) {$ file_con = fread ($ fp, $ buffer); $ file_count + = $ buffer; echo $ file_con;} www. jbxue. comfclose ($ fp) ;}?>
PHP mandatory File Download source code
It provides users with mandatory File Download functions.
/*********************@file - path to file*/function force_download($file){if ((isset($file))&&(file_exists($file))) {header("Content-length: ".filesize($file));header('Content-Type: application/octet-stream');header('Content-Disposition: attachment; filename="' . $file . '"');readfile("$file");} else {echo "No file selected";}}
Are you sure you want to laugh at me and say "download files" is that simple? Of course it is not as simple as imagined. For example, if you want the customer to fill out a form before downloading a file, your first thought must be "Redirect, first, check whether the form is complete and complete, and then point the website to this file so that the customer can download it. However, if you want to create an e-commerce website for "online shopping, for security concerns, you do not want users to directly copy the website to download the file. I suggest you use PHP to directly read the actual file and then download it. The procedure is as follows:
$ File_name = "info_check.exe"; $ file_dir = "/public/www/download/"; if (! File_exists ($ file_dir. $ file_name) {// check whether the file exists. echo "the file cannot be found"; exit;} else {$ file = fopen ($ file_dir. $ file_name, "r"); // open the file // enter the file tag www. jbxue. comHeader ("Content-type: application/octet-stream"); Header ("Accept-Ranges: bytes"); Header ("Accept-Length :". filesize ($ file_dir. $ file_name); Header ("Content-Disposition: attachment; filename = ". $ file_name); // echo fread ($ file, filesize ($ file_dir. $ file_name); fclose ($ file); exit ;}
If the file path is "http" or "ftp", the source code will change a little. The program is as follows:
$ File_name = "info_check.exe"; $ file_dir = "http://www.jbxue.com/"; $ file = @ fopen ($ file_dir. $ file_name, "r"); if (! $ File) {echo "file not found";} else {Header ("Content-type: application/octet-stream"); Header ("Content-Disposition: attachment; filename = ". $ file_name); while (! Feof ($ file) {echo fread ($ file, 50000) ;}fclose ($ file );}
In this way, you can use PHP to directly output files.
However, you must note that the Header information is equivalent to first uploading the file information to the high-speed browser, and then downloading the information on the browser to the attachment. Therefore, in an MVC-mode application, the view page must not contain any content. Otherwise, the view page content will be downloaded along with the file content, as a result, the downloaded file cannot be used.
The following is my program:
public function downloadAction(){if (isset($_GET['mriID'])){$this->view->mriID=(get_magic_quotes_gpc())?$_GET['mriID']:addslashes($_GET['mriID']);}if (isset($_GET['dicomID'])){$this->view->dicomID=(get_magic_quotes_gpc())?$_GET['dicomID']:addslashes($_GET['dicomID']);}if (isset($_GET['JPGID'])){$this->view->JPGID=(get_magic_quotes_gpc())?$_GET['JPGID']:addslashes($_GET['JPGID']);} www.jbxue.com$dicomfile=new dicomfile(); $jpgfile=new jpgfile();$mri=new mri();if($this->view->dicomID){$filename=$dicomfile->find($this->view->dicomID)->toArray();$filename=$filename[0]['filename'];} else if($this->view->JPGID){$filename=$jpgfile->find($this->view->JPGID)->toArray();$filename=$filename[0]['JPGname'];}$dir=$mri->find($this->view->mriID)->toArray();$dir=$dir[0]['dicom_path'];$file=$dir.'/'.$filename;if (!file_exists($file)){echo "the file does not exist!";exit();}$file_size=filesize($file);header("Content-type: application/octet-stream");header("Accept-Ranges: bytes");header("Accept-Length:". $file_size);header("Content-Disposition: attachment; filename=".$filename); $fp=fopen($file,"r");if (!$fp)echo "can't open file!";$buffer_size=1024;$cur_pos=0;while (!feof($fp)&&$file_size-$cur_pos>$buffer_size){$buffer=fread($fp,$buffer_size);echo $buffer;$cur_pos+=$buffer_size;}$buffer=fread($fp,$file_size-$cur_pos);echo $buffer;fclose($fp); }
In this case, the download. phtml page must be completely blank. Do not have any content (including the following fixed information:
<! DOCTYPE html PUBLIC "-// W3C // dtd xhtml 1.0 Transitional // EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">