Home >Backend Development >PHP Tutorial >Code example of php readfile function downloading files and determining permissions

Code example of php readfile function downloading files and determining permissions

WBOY
WBOYOriginal
2016-07-25 08:58:501198browse
  1. /**
  2. * Application examples of header and readfile functions
  3. * Download files and determine permissions
  4. * edit bbs.it-home.org
  5. */
  6. $file = get_file_address();// The real address of the file (supports url, but url is not recommended)
  7. if (file_exists($file ))
  8. {
  9. header('Content-Description: File Transfer');
  10. header('Content-Type: application/octet-stream');
  11. header('Content-Disposition: attachment; filename='.basename($ file));
  12. header('Content-Transfer-Encoding: binary');
  13. header('Expires: 0');
  14. header('Cache-Control: must-revalidate, post-check=0, pre-check= 0');
  15. header('Pragma: public');
  16. header('Content-Length: ' . filesize($file));
  17. ob_clean(); //Pay attention to the call of this function, clear but not close the output cache , otherwise the first two characters of the downloaded file will be 0a
  18. flush();
  19. readfile($file); // Output file content
  20. }
  21. ?>
Copy code


Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn