PHP mandatory file download

WBOY
Release: 2016-07-25 08:42:39
Original
888 people have browsed it

If you need to download a specific file without opening a new window, the following code snippet can help you.

  1. function force_download($file)
  2. {
  3. $dir = "../log/exports/";
  4. if ((isset($file))&&(file_exists($dir.$file))) {
  5. header("Content-type: application/force-download");
  6. header('Content-Disposition: inline; filename="' . $dir.$file . '"');
  7. header("Content-Transfer- Encoding: Binary");
  8. header("Content-length: ".filesize($dir.$file));
  9. header('Content-Type: application/octet-stream');
  10. header('Content-Disposition: attachment; filename="' . $file . '"');
  11. readfile("$dir$file");
  12. } else {
  13. echo "No file selected";
  14. }
  15. }
Copy code

Usage:

  1. force_download("image.jpg");
  2. ?>
Copy code

PHP


Related labels:
source:php.cn
Statement of this Website
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
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template
About us Disclaimer Sitemap
php.cn:Public welfare online PHP training,Help PHP learners grow quickly!