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

Source: Internet
Author: User
Tags php download
Sometimes we need to use php to download some files. this is generally required to hide the files. Otherwise, it will increase the burden on the server. it is better to directly provide the software address and download the source code for a simple php file, although resumable data transfer is not supported, it can meet some common requirements. Php download file is actually implemented with a tag, such as magento-1.8.1.0.zip. However, when a page view can identify the format, such as. txt,. html,. txt, and so on, then use abc.txt to know what will happen.

The code is as follows:


/**
* 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.

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

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

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

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



Untitled DocumentOtherwise, all the information will be downloaded to the downloaded file, and the file cannot be used.

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.