For a “force download script” to which i send as variable a full URL, i needed to convert this url into the local path, so that the system can check whether the file exists or not, and then read it to output with the correct mimetype.

Here is what i came up with. It seems simple but i couldn’t find such an easy way anywhere on the web so here is my modest contribution to a better (documented) world :-).

PHP: convertion of a full url to the file’s corresponding local system path

[php]
// EXAMPLELINK http://www.domain.com/files/downloads.php?file=http://localhost/lab-au.com/dev/medias/03-05-2008_bright-archite/pdf/xxx___Labau.pdf
$filename = urldecode($_GET[‘filename’]);
$parsed_url= parse_url($filename);
$fileinfo = pathinfo($filename);
$parsed_url[‘extension’] = $fileinfo[‘extension’];
$parsed_url[‘filename’] = $fileinfo[‘basename’];
$parsed_url[‘localpath’] = $_SERVER[‘DOCUMENT_ROOT’].$parsed_url[‘path’];
// just in case there is a double slash created when joining document_root and path
$parsed_url[‘localpath’] = preg_replace(‘/\/\//’, ‘/’, $parsed_url[‘localpath’]);

[/php]

Force Download Php Script

The full “force download” script is to be used like this :

http://path/to/force/downloadscript.php?file=http://path/to/your/file.pdf

[php]

/*
// PHP FORCE DOWNLOAD SCRIPT
author: pixeline
website: http://www.pixeline.be
last updated: 14 January 2009
*/
$filename = urldecode($_GET[‘filename’]);
// converting url to local path so Apache can find the file.
// force download:
// required for IE, otherwise Content-disposition is ignored
if (ini_get(‘zlib.output_compression’))
ini_set(‘zlib.output_compression’, ‘Off’);

$parsed_url = parse_url($filename);
$fileinfo = pathinfo($filename);
$parsed_url[‘extension’] = $fileinfo[‘extension’];
$parsed_url[‘filename’] = $fileinfo[‘basename’];
$parsed_url[‘localpath’] = $_SERVER[‘DOCUMENT_ROOT’] . $parsed_url[‘path’];
// just in case there is a double slash created when joining document_root and path
$parsed_url[‘localpath’] = preg_replace(‘/\/\//’, ‘/’, $parsed_url[‘localpath’]);

if (!file_exists($parsed_url[‘localpath’])) {
die(‘File not found: ‘ . $parsed_url[‘localpath’]);
}
$allowed_ext = array(‘pdf’, ‘png’, ‘jpg’, ‘jpeg’, ‘zip’, ‘doc’, ‘xls’, ‘gif’, ‘exe’, ‘ppt’);
if (!in_array($parsed_url[‘extension’], $allowed_ext)) {
die(‘This file type is forbidden.’);
}

switch ($parsed_url[‘extension’]) {
case “pdf”: $ctype = “application/pdf”;
break;
case “exe”: $ctype = “application/octet-stream”;
break;
case “zip”: $ctype = “application/zip”;
break;
case “doc”: $ctype = “application/msword”;
break;
case “xls”: $ctype = “application/vnd.ms-excel”;
break;
case “ppt”: $ctype = “application/vnd.ms-powerpoint”;
break;
case “gif”: $ctype = “image/gif”;
break;
case “png”: $ctype = “image/png”;
break;
case “jpeg”:
case “jpg”: $ctype = “image/jpg”;
break;
default: $ctype = “application/force-download”;
}
header(“Pragma: public”); // required
header(“Expires: 0”);
header(“Cache-Control: must-revalidate, post-check=0, pre-check=0”);
header(“Cache-Control: private”, false); // required for certain browsers
header(“Content-Type: $ctype”);
header(“Content-Disposition: attachment; filename=\”” . $parsed_url[‘filename’] . “\”;”);
header(“Content-Transfer-Encoding: binary”);
header(“Content-Length: ” . filesize($parsed_url[‘localpath’]));
readfile($parsed_url[‘localpath’]);
exit();

[/php]

[ad#horizontal_inline]