Azharkamar.com

Left Click ‘Save Target As’ PHP Script

SnippetsTutorials
8 Mar 2011
Comment

Here’s an easy peezy PHP script to mimic the Right click > Save Target As… (or Save Link As, Save Image As, etc.) action in browsers. This script allows you to simply left click and the file dialog box will pop out.

<?php
$filename = $_GET['filename'];
if( ! is_file($filename) || $filename[0] == '.' || $filename[0] == '/' )
die("Bad access attempt.\n");
$file = explode('.', $filename);
if($file[1] != 'pdf') //define file extension for security
die('Could not download selected file.');
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Content-Type: application/force-download");
header("Content-Type: application/octet-stream");
header("Content-Type: application/download");
header("Content-Disposition: attachment; filename=".basename($filename).";");
header("Content-Transfer-Encoding: binary");
header("Content-Length: ".filesize($filename));
readfile("$filename");
exit();
?>

Author - Azhar Kamar

Write a Comment! NOW! :)

Current ye@r *