PHP download file code [customizable]-php Tutorial

Source: Internet
Author: User
Tags php download
PHP download file code [customizable]

  1. Use the get method to pass in the http: // domain/download. php? parameter? File = 'savepath on server' & name = 'File name you want'
  2. // Input the file path and file name, which are used to read and rename the file respectively.
  3. If (empty ($ _ GET ['file']) | empty ($ _ GET ['name']) {
  4. Echo 'script alert ("no file found .....! "); Location. replace (" index. php ") script '; exit ();
  5. }
  6. // File path
  7. $ File = $ _ GET ['file'];
  8. // File name [that is, the file title, not the name saved on the server]
  9. $ Name = $ _ GET ['name'];
  10. // Obtain the file suffix
  11. $ Suffix = substr (strrchr ($ file, '.'), 1 );
  12. If (is_file ('./'. $ file )){
  13. Header ("Content-Type: application/force-download ");
  14. Header ("Content-Disposition: attachment; filename =". $ name. '.'. $ suffix );
  15. Readfile ('./'. $ file );
  16. Exit;
  17. } Else {
  18. Echo "the file does not exist! ";
  19. Exit;
  20. }
  21. ?>


PHP

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.