Mnogo prosto. U .htaccess stavis sledece:
RewriteEngine On
RewriteCond %{QUERY_STRING} !(^|&)secured=true($|&)
RewriteRule download/(.+) download.php [nc,L]
S tim sto "secured=true" mozes da zamenis cime god hoces. U download.php stavis sledece:
<?php
session_start();
function get_mime_type($file)
{
$mime_types = array(
"pdf"=>"application/pdf",
"exe"=>"application/octet-stream",
"zip"=>"application/zip",
"rar"=>"application/rar",
"docx"=>"application/msword",
"doc"=>"application/msword",
"xls"=>"application/vnd.ms-excel",
"ppt"=>"application/vnd.ms-powerpoint",
"gif"=>"image/gif",
"png"=>"image/png",
"jpeg"=>"image/jpg",
"jpg"=>"image/jpg",
"mp3"=>"audio/mpeg",
"wav"=>"audio/x-wav",
"mpeg"=>"video/mpeg",
"mpg"=>"video/mpeg",
"mpe"=>"video/mpeg",
"mov"=>"video/quicktime",
"avi"=>"video/x-msvideo",
"3gp"=>"video/3gpp",
"css"=>"text/css",
"jsc"=>"application/javascript",
"js"=>"application/javascript",
"php"=>"text/html",
"htm"=>"text/html",
"html"=>"text/html"
);
$parts = explode('.',$file);
$extension = strtolower(end($parts));
return $mime_types[$extension];
}
// Ovde proveris da li sme da pridje fajlu, nesto tipa
if (!$_SESSION['logged_in'])
{
die('Nece moci ove noci!');
}
// Odavde nanize ide prikaz/download fajla (u zavisnosti od tipa)
$file = $_SERVER['REQUEST_URI'];
$path = 'http://' . $_SERVER['HTTP_HOST'];
$content = file_get_contents($path.$file.'?secured=true'); // ako si menjao "secured=true" u htaccess-u, zameni ga i ovde
$mime_type = get_mime_type($path.$file);
header('Content-type: ' . $mime_type);
echo $content;
die();
I vozi Misko
Btw, funkcija get_mime_type je dodata jer je mime_content_type() deprecated, a FILE_INFO ekstenzija mozda nije dostupna na hostu koji koristis.
|