PHP gevorderde |
|
Geen van de mogelijkheden hierboven bieden wat jij graag wilt; gemakkelijk per gebruiker aan kunnen geven of ze de bestanden mogen downloaden.
Terwijl daar een heel makkelijke manier voor is!
De PHP code is als volgt:
<?php
// Na een check of de gebruiker deze mag downloaden
// (dit ga ik hier niet simuleren, dat kun je zelf wel ;) )
// doe je dit:
$nieuwe_naam = 'nieuwe_naam';
$locatie_bestand = 'beschermde_map/download1.zip';
// Het bestand 'serveren':
serveFile($locatie_bestand, $nieuwe_naam);
?>
<?php // Na een check of de gebruiker deze mag downloaden // (dit ga ik hier niet simuleren, dat kun je zelf wel ;) ) // doe je dit: $nieuwe_naam = 'nieuwe_naam'; $locatie_bestand = 'beschermde_map/download1.zip'; // Het bestand 'serveren': serveFile($locatie_bestand, $nieuwe_naam); ?>
Zoals je wel ziet wordt hier de functie serveFile aangeroepen, die zit niet standaard in PHP, die heb ik zelf gemaakt, en ziet er als volgt uit:
<?php
function serveFile($original, $newname)
{
// For IE
if (ini_get('zlib.output_compression'))
ini_set('zlib.output_compression', 'Off');
// Get the extension
$extension = strtolower(substr(strrchr($original, '.'), 1));
// Get the content-type
switch ($extension) {
case "pdf": $ctype = 'application/pdf'; break;
case "exe": $ctype = 'application/octet-stream'; break;
case "zip": $ctype = 'application/zip'; break;
case "doc": case "docx": $ctype = 'application/msdos'; break;
case "xls": case "xlsx": $ctype = ' application/vnd.ms-excel'; break;
case "xml": $ctype = 'application/xml'; break;
case "avi": $ctype = 'video/avi'; break;
case "bmp": $ctype = 'image/bmp'; break;
case "css": $ctype = 'text/css'; break;
case "gif": $ctype = 'image/gif'; break;
case "png": $ctype = 'image/png'; break;
case "jpg": case "jpeg": $ctype = 'image/jpeg'; break;
case "ppt": case "pptx": $ctype = 'application/vnd.ms-powerpoint'; break;
case "hlp": case "help": $ctype = 'application/x-helpfile'; break;
case "html": case "htm": $ctype = 'text/html'; break;
case "txt": case "ini": case "idc": case "hh": case "h":
case "jav": case "java": case "list": case "log": case "lst":
case "m": case "mar": case "pl": case "sdml": case "text": case "c":
case "c++": case "cc": case "com": case "conf": case "cxx": case "def": case "php":
case "f": case "f90": case "for": case "g": case "bat": $ctype = 'text/plain'; break;
case "rar": $ctype = 'application/zip'; break;
default: $ctype = 'text/plain'; break;
}
// Check if headers were sent, if so, abort
if (headers_sent())
trigger_error('headers already sent', E_USER_ERROR);
// Check if the original exists
if (!is_file($original))
trigger_error('file not found', E_USER_ERROR);
// Send some headers
header('Pragma: public');
header('Expires: 0');
header('Cache-control: must-revalidate; post-check=0, pre-check=0');
header('Cache-control: private', false);
header('Content-Type:' . $ctype);
header('Content-Disposition: attachment; filename="' . basename($newname) . '.' . $extension . '"');
header('Content-Transfer-Encoding: binary');
header('Content-Length: ' . filesize($original));
// Read and output the file's contents
readfile($original);
// Stop output, to prevent the file from being corrupted
exit;
}
?>
<?php function serveFile($original, $newname) { // For IE if (ini_get('zlib.output_compression')) ini_set('zlib.output_compression', 'Off'); // Get the extension // Get the content-type switch ($extension) { case "pdf": $ctype = 'application/pdf'; break; case "exe": $ctype = 'application/octet-stream'; break; case "zip": $ctype = 'application/zip'; break; case "doc": case "docx": $ctype = 'application/msdos'; break; case "xls": case "xlsx": $ctype = ' application/vnd.ms-excel'; break; case "xml": $ctype = 'application/xml'; break; case "avi": $ctype = 'video/avi'; break; case "bmp": $ctype = 'image/bmp'; break; case "css": $ctype = 'text/css'; break; case "gif": $ctype = 'image/gif'; break; case "png": $ctype = 'image/png'; break; case "jpg": case "jpeg": $ctype = 'image/jpeg'; break; case "ppt": case "pptx": $ctype = 'application/vnd.ms-powerpoint'; break; case "hlp": case "help": $ctype = 'application/x-helpfile'; break; case "html": case "htm": $ctype = 'text/html'; break; case "txt": case "ini": case "idc": case "hh": case "h": case "jav": case "java": case "list": case "log": case "lst": case "m": case "mar": case "pl": case "sdml": case "text": case "c": case "c++": case "cc": case "com": case "conf": case "cxx": case "def": case "php": case "f": case "f90": case "for": case "g": case "bat": $ctype = 'text/plain'; break; case "rar": $ctype = 'application/zip'; break; default: $ctype = 'text/plain'; break; } // Check if headers were sent, if so, abort // Check if the original exists // Send some headers header('Cache-control: must-revalidate; post-check=0, pre-check=0'); header('Cache-control: private', false); header('Content-Type:' . $ctype); header('Content-Disposition: attachment; filename="' . basename($newname) . '.' . $extension . '"'); header('Content-Transfer-Encoding: binary'); // Read and output the file's contents // Stop output, to prevent the file from being corrupted } ?>
Als je de map waarin de bestanden staan dan ook nog eens blokkeerd voor alle IP's (dat maakt niet uit voor het script, die kan er evengoed nog bij), kunnen alleen de mensen die de toegang hebben bij het bestand. |