Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- To force download an image and not to be opened in the browser reaches the following script:
- sample code:
- <?PHP
- $file = "test.jpg";
- header("Pragma: public");
- header("Expires: 0");
- header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
- header("Content-Type: application/octet-stream");
- header("Content-Disposition: attachment; filename=$file");
- header("Content-Transfer-Encoding: binary");
- readfile( $file);
- exit;
- ?>
- Caution! with the script, a visitor can also download PHP files etc..
- If you get the variable from a form, you need to install a lock
- <?PHP
- $file = "test.jpg";
- $allow_files = Array(".jpg", ".gif", ".png");
- if (!strpos($file,$allow_files))
- {
- header("Pragma: public");
- header("Expires: 0");
- header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
- header("Content-Type: application/octet-stream");
- header("Content-Disposition: attachment; filename=$file");
- header("Content-Transfer-Encoding: binary");
- readfile( $file );
- exit;
- }
- ?>
Add Comment
Please, Sign In to add comment