PHP download file to force arbitrary file format Download _php tutorial

Source: Internet
Author: User
Tags fread php download ranges
download some files in PHP, usually to hide the file's true download address only need this, otherwise this will increase the burden on the server, rather than directly provide the address of the software.

A simple php file download source code, although not support the continuation of the breakpoint, but can meet some common needs. PHP download files can actually be implemented with a tag, such as However, some browsers can recognize the format, such as. txt,.html,.pdf, and then use Abc.txt presumably also know what will happen.

 Php/** * File Download * **/Header("Content-type:text/html;charset=utf-8");d ownload (' Web/ ', ' Magento download '); functionDownload$file,$down _name){$suffix=substr($file,Strrpos($file,'.'));//get file suffix$down _name=$down _name.$suffix;//The new file name is the name after the download//To determine whether a given file exists or notif(!file_exists($file)){ die("The file you want to download no longer exists, it may be deleted");} $fp=fopen($file, "R");$file _size=filesize($file);//the header to use to download the fileHeader("Content-type:application/octet-stream");Header("Accept-ranges:bytes");Header("Accept-length:".)$file _size);Header("Content-disposition:attachment; Filename= ".$down _name);$buffer= 1024;$file _count= 0;//returning 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);}?>

Source code for PHP mandatory file download

To provide users with mandatory file download function.

/* ******************** @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 $file . '"'); ReadFile ("$file"else  {echo "No file selected";}}

You're going to laugh at me. "Download file" So simple is worth saying? Of course it's not as simple as imagination. For example, you want customers to fill out a form, you can download a file, your first thought must be "Redirect" method, first check whether the form has been completed and complete, and then the URL refers to the file, so that customers can download, but if you want to do a "online shopping" e-commerce site , consider the security issues, you do not want users to directly copy the URL to download the file, I suggest you use PHP directly read the actual file and then download the method to do. The procedure is as follows:

$file _name= "Info_check.exe";$file _dir= "/public/www/download/";if(!file_exists($file _dir.$file _name)) {//Check if the file existsEcho"File not Found";Exit;} Else {$file=fopen($file _dir.$file _name, "R");//Open File//Input File tab 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);//Output File ContentsEcho fread($file,filesize($file _dir.$file _name));fclose($file);Exit;} 

If the file path is "http" or "FTP" URL, then the source code will change a little, the program is as follows:

$file _name = "Info_check.exe"; $file _dir = ""; $file fopen ($file _dir$file _name, "R"); if (! $file {echo "file cannot find"else  {Header("Content-type: Application/octet-stream "); Header $file _name );  while (! feof ($file)) {echofread($file, 50000);} fclose ($file

This allows you to output the file directly with PHP.

However, it is important to note that the header information is equivalent to the file information high-speed browser, and then to download the information on the browser to the attachment. Therefore, if the view page must not have any content in the MVC mode application, the content of the view page will be downloaded along with the contents of the file, causing the downloaded file to become unused.
Here is my program:

 Public functiondownloadaction () {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 ']);}$dicomfile=Newdicomfile ();$jpgfile=Newjpgfile ();$mri=NewMRI ();if($this->view->Dicomid) {$filename=$dicomfile->find ($this-&GT;VIEW-&GT;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-&GT;VIEW-&GT;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); }

At this point, the download.phtml page must be completely blank. Never have any content (including the following fixed information:

Public "-//W3C//DTD XHTML 1.0 transitional//en" "Http://" >
  Untitled DocumentOtherwise, this information will be downloaded to the download file, causing the file to not be used. true techarticle download some files in PHP, usually to hide the file's true download address only need this, otherwise this will increase the burden on the server, rather than directly provide the address of the software. A simple ...

  • Related Article

    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: 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.