Forum Moderators: coopster
@d40sithui: I would definitely be interested in seeing some code for that method, thank you. I assume you mean above htdocs when you say above my website directory right? I've always been interested by this, as a few people have talked about placing things like the .htaccess doc there, but I've wondered how you reference it in source code.
@henry0: could you explain a bit more what you mean by benchmarking my way against a usual url please?
What I need really, is just a simple way to avert the average user from downloading my media files. I presume I should be 'protecting' the directory the files are in with an index file as well right?
Thanks
//if user has valid access to the file
if (validUser()) {
//some db queries here
$downloadBaseDir = "/var/www/myDownloads/"; //directory where the file is located
$file_path ='';
$file_path = find_file($downloadBaseDir, $fname, $file_path);
if(!is_file($file_path)){
header('HTTP/1.0 404 Not Found');
}
$fsize=filesize($file_path);
//set m-type
$mtype = '';
// mime type is not set, get from server settings
if (function_exists('mime_content_type')) {
$mtype = mime_content_type($file_path);
}
else if (function_exists('finfo_file')) {
$finfo = finfo_open(FILEINFO_MIME); // return mime type
$mtype = finfo_file($finfo, $file_path);
finfo_close($finfo);
}
if ($mtype == '') {
$mtype = "application/force-download";
}
$fname = preg_replace("/\//", "", $fname);
// set headers
header("Pragma: public");
header("Expires: 0");
header("Cache-Control: must-revalidate, post-check=0, pre-check=0");
header("Cache-Control: public");
header("Content-Description: File Transfer");
header("Content-Type: $mtype");
header("Content-Disposition: attachment; filename=\"$fname\"");
header("Content-Transfer-Encoding: binary");
header("Content-Length: " . $fsize);
// download
// @readfile($file_path);
$file = @fopen($file_path,"rb");
if ($file) {
while(!feof($file)) {
print(fread($file, 1024*8));
flush();
if (connection_status()!=0) {
@fclose($file);
die();
}
}
@fclose($file);
}
}
//invalid access
else {
header('HTTP/1.0 404 Not Found');
}
a few notes. starting off, i check if the user has access to the file. if not, it will load a HTTP 404 error page using header. if the user is valid, we do some sql queries, which are not shown here to find the file's information such as its path ($file_path) and file name($fname). after that is just some basic check to see if the file exists. i use a function find_file to do so which basically uses several core php functions such as file_exists() and is_dir(). kinda redundant, since the next line checks if the file is a file lol. next find the size and the type of file by using fsize() and mime_content_type() with the $file_path as the parameter.
after that, you can set the headers and open the file. as an example my website directly is "/var/www/html" while the files are located in "/var/www/myDownloads". you can have subdirectories in the download file. just when you add a file to the db you'd probably need to name the file_path with the folder name - "myFolder/myFile.zip" as appose to just "myFile.zip." anyways hopes this helps.