irwan

Force download a image

Apr 18th, 2012
105
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
PHP 0.99 KB | None | 0 0
  1.  
  2. To force download an image and not to be opened in the browser reaches the following script:
  3.  
  4. sample code:
  5. <?PHP
  6. $file = "test.jpg";
  7. header("Pragma: public");
  8. header("Expires: 0");
  9. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  10. header("Content-Type: application/octet-stream");
  11. header("Content-Disposition: attachment; filename=$file");
  12. header("Content-Transfer-Encoding: binary");
  13. readfile( $file);
  14. exit;
  15. ?>
  16.  
  17. Caution! with the script, a visitor can also download PHP files etc..
  18. If you get the variable from a form, you need to install a lock
  19. <?PHP
  20. $file = "test.jpg";
  21. $allow_files = Array(".jpg", ".gif", ".png");
  22. if (!strpos($file,$allow_files))
  23. {
  24. header("Pragma: public");
  25. header("Expires: 0");
  26. header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
  27. header("Content-Type: application/octet-stream");
  28. header("Content-Disposition: attachment; filename=$file");
  29. header("Content-Transfer-Encoding: binary");
  30. readfile( $file );
  31. exit;
  32. }
  33. ?>
Add Comment
Please, Sign In to add comment