Funktion: Anhand der File ID holt er sich die entsprechenden Informationen aus der Datenbank, setzt die Header und "fordert" den Browser zum Dateidownload auf.
Es wird überprüft, ob es eine Datei und kein Ordner ist, die Datei veröffentlicht wurde und die Datei durchsuchbar ist (Hauptkriterium zur Beschränkung).
PHP Code:
Code: Alles auswählen
/**
* check, if passed key is a valid integer
*/
$file_id = filter_var(key($_GET), FILTER_SANITIZE_NUMBER_INT);
if(empty($file_id) === false)
{
/**
* get file information from database
* Crits: Searchable, no folder, published
*/
$sql = '
SELECT
*
FROM
`tblFile`
WHERE
`ID` = ' . $db->real_escape_string($file_id) . '
AND
`IsSearchable` = 1
AND
`IsFolder` = 0
AND
`Published` != 0
LIMIT 1;';
$result = $db->query($sql);
/**
* check, if entry exists
*/
if($result->num_rows === 1)
{
$data = $result->fetch_assoc();
$file = $_SERVER['DOCUMENT_ROOT'] . $data['Path'];
/**
* check if file exists
*/
if(is_file($file))
{
$contentType = $data['ContentType'];
if(substr($contentType, -1) === '*')
{
$contentType = substr($contentType, 0, -1) . substr($data['Extension'], 1);
}
/**
* output the file
*/
header('Content-Type: ' . $contentType);
header('Content-Transfer-Encoding: binary');
header('Content-Length:' . filesize($file));
header('Content-Disposition: attachment; filename=' . $data['Filename']);
readfile($file);
}
else
{
echo 'file not found.';
}
}
else
{
echo 'file not found.';
}
}