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 provide the software address directly.
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 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 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 complete 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.