Php download file source code (Force download of any file format)

Source: Internet
Author: User
Tags php download

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.

Copy codeThe Code is as follows:
<? 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; // the new file name is the downloaded name.

// Determine whether a specified file exists
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 );
// The header used to download the object
Header ("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;
// Return data to the browser
While (! Feof ($ fp) & $ file_count <$ file_size ){
$ File_con = fread ($ fp, $ buffer );
$ File_count + = $ buffer;
Echo $ file_con;
}
Fclose ($ fp );
}
?>

PHP mandatory File Download source code

It provides users with mandatory File Download functions.
Copy codeThe Code is as follows:
/********************
* @ 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:

Copy codeThe Code 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 "file not found ";
Exit;
} Else {
$ File = fopen ($ file_dir. $ file_name, "r"); // open the file
// Input file tag
Header ("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 content
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:

Copy codeThe Code is as follows:
$ File_name = "info_check.exe ";
$ File_dir = "http://www.jb51.net /";
$ 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:

 

Copy codeThe Code is as follows:
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']);
}
$ 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] ['jpg name'];
}
$ 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">
<Html xmlns = "http://www.w3.org/1999/xhtml">
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8"/>
<Title> untitled document </title>) otherwise, the information will be downloaded to the downloaded file, making the file unusable.

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: info-contact@alibabacloud.com 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.