Php force download of any file format

Source: Internet
Author: User
Tags php download
Php force download of any file format

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

  1. /**
  2. * File download
  3. *
  4. **/
  5. Header ("Content-type: text/html; charset = utf-8 ");
  6. Download ('web/magento-1.8.1.0.zip ', 'magento download ');
  7. Function download ($ file, $ down_name ){
  8. $ Suffix = substr ($ file, strrpos ($ file, '.'); // get the file suffix
  9. $ Down_name = $ down_name. $ suffix; // The new file name is the downloaded name.
  10. // Determine whether a specified file exists
  11. If (! File_exists ($ file )){
  12. Die ("the file you want to download does not exist, it may be deleted ");
  13. }
  14. $ Fp = fopen ($ file, "r ");
  15. $ File_size = filesize ($ file );
  16. // The header used to download the object
  17. Header ("Content-type: application/octet-stream ");
  18. Header ("Accept-Ranges: bytes ");
  19. Header ("Accept-Length:". $ file_size );
  20. Header ("Content-Disposition: attachment; filename =". $ down_name );
  21. $ Buffer = 1024;
  22. $ File_count = 0;
  23. // Return data to the browser
  24. While (! Feof ($ fp) & $ file_count <$ file_size ){
  25. $ File_con = fread ($ fp, $ buffer );
  26. $ File_count + = $ buffer;
  27. Echo $ file_con;
  28. } Www.jbxue.com
  29. Fclose ($ fp );
  30. }
  31. ?>

PHP mandatory file download source code

It provides users with mandatory file download functions.

  1. /********************
  2. * @ File-path to file
  3. */
  4. Function force_download ($ file)
  5. {
  6. If (isset ($ file) & (file_exists ($ file ))){
  7. Header ("Content-length:". filesize ($ file ));
  8. Header ('content-Type: application/octet-stream ');
  9. Header ('content-Disposition: attachment; filename = "'. $ file .'"');
  10. Readfile ("$ file ");
  11. } Else {
  12. Echo "No file selected ";
  13. }
  14. }

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:

  1. $ File_name = "info_check.exe ";
  2. $ File_dir = "/public/www/download /";
  3. If (! File_exists ($ file_dir. $ file_name) {// check whether the file exists
  4. Echo "file not found ";
  5. Exit;
  6. } Else {
  7. $ File = fopen ($ file_dir. $ file_name, "r"); // open the file
  8. // Enter the file tag www.jbxue.com
  9. Header ("Content-type: application/octet-stream ");
  10. Header ("Accept-Ranges: bytes ");
  11. Header ("Accept-Length:". filesize ($ file_dir. $ file_name ));
  12. Header ("Content-Disposition: attachment; filename =". $ file_name );
  13. // Output file content
  14. Echo fread ($ file, filesize ($ file_dir. $ file_name ));
  15. Fclose ($ file );
  16. Exit;
  17. }

If the file path is "http" or "ftp", the source code will change a little. The program is as follows:

  1. $ File_name = "info_check.exe ";
  2. $ File_dir = "http://www.jbxue.com /";
  3. $ File = @ fopen ($ file_dir. $ file_name, "r ");
  4. If (! $ File ){
  5. Echo "file not found ";
  6. } Else {
  7. Header ("Content-type: application/octet-stream ");
  8. Header ("Content-Disposition: attachment; filename =". $ file_name );
  9. While (! Feof ($ file )){
  10. Echo fread ($ file, 50000 );
  11. }
  12. Fclose ($ file );
  13. }

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:

  1. Public function downloadAction ()
  2. {
  3. If (isset ($ _ GET ['mriid'])
  4. {
  5. $ This-> view-> mriID = (get_magic_quotes_gpc ())? $ _ GET ['mriid']: addslashes ($ _ GET ['mriid']);
  6. }
  7. If (isset ($ _ GET ['dicomid'])
  8. {
  9. $ This-> view-> dicomID = (get_magic_quotes_gpc ())? $ _ GET ['dicomid']: addslashes ($ _ GET ['dicomid']);
  10. }
  11. If (isset ($ _ GET ['jpgid'])
  12. {
  13. $ This-> view-> JPGID = (get_magic_quotes_gpc ())? $ _ GET ['jpgid']: addslashes ($ _ GET ['jpgid']);
  14. } Www.jbxue.com
  15. $ Dicomfile = new dicomfile ();
  16. $ Jpgfile = new jpgfile ();
  17. $ Mri = new mri ();
  18. If ($ this-> view-> dicomID)
  19. {
  20. $ Filename = $ dicomfile-> find ($ this-> view-> dicomID)-> toArray ();
  21. $ Filename = $ filename [0] ['filename'];
  22. }
  23. Else if ($ this-> view-> JPGID)
  24. {
  25. $ Filename = $ jpgfile-> find ($ this-> view-> JPGID)-> toArray ();
  26. $ Filename = $ filename [0] ['jpg name'];
  27. }
  28. $ Dir = $ mri-> find ($ this-> view-> mriID)-> toArray ();
  29. $ Dir = $ dir [0] ['dicom _ path'];
  30. $ File = $ dir. '/'. $ filename;
  31. If (! File_exists ($ file ))
  32. {
  33. Echo "the file does not exist! ";
  34. Exit ();
  35. }
  36. $ File_size = filesize ($ file );
  37. Header ("Content-type: application/octet-stream ");
  38. Header ("Accept-Ranges: bytes ");
  39. Header ("Accept-Length:". $ file_size );
  40. Header ("Content-Disposition: attachment; filename =". $ filename );
  41. $ Fp = fopen ($ file, "r ");
  42. If (! $ Fp)
  43. Echo "can't open file! ";
  44. $ Buffer_size = 1024;
  45. $ Cur_pos = 0;
  46. While (! Feof ($ fp) & $ file_size-$ cur_pos> $ buffer_size)
  47. {
  48. $ Buffer = fread ($ fp, $ buffer_size );
  49. Echo $ buffer;
  50. $ Cur_pos + = $ buffer_size;
  51. }
  52. $ Buffer = fread ($ fp, $ file_size-$ cur_pos );
  53. Echo $ buffer;
  54. Fclose ($ fp );
  55. }

In this case, the download. phtml page must be completely blank. Do not have any content (including the following fixed information:

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