php: force download script + convert url to localpath
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
-
// 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
-
$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
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 FORCE DOWNLOAD SCRIPT
-
author: pixeline
-
website: http://www.pixeline.be
-
last updated: 14 January 2009
-
*/
-
// converting url to local path so Apache can find the file.
-
// force download:
-
// required for IE, otherwise Content-disposition is ignored
-
-
$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
-
-
}
-
}
-
-
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";
-
}
Author: pixeline
Date: January 14th, 2009
filed in: Development, SEO
Follow the discussion on this entry via RSS 2.0 feed.
I try your code and have error on this code:
#
if (!file_exists($parsed_url['localpath'])) {
#
die('File not found: ' . $parsed_url['localpath']);
#
}
why?
what is the error message you get?