Reputation:
I'm trying a download of a protected zip, rar file. After trying hundreds of different headers and combinations, I'm lost. I cann download images, pdf but zip and rar are corrupted.
Where is the problem?
Code
$url = url_for('/staff/photos/error.php');
$error = $url;
//$filepath = 'C:\wamp\www\photo_gallery\public\files\3d_models';
$url2 = PROJECT_PATH.DS.'public'.DS.'files'.DS.'3d_models';
$filepath = $url2;
$filename = null;
if (isset($_GET['file']) && basename($_GET['file']) == $_GET['file']) {
$filename = $_GET['file'];
} else {
header("Location: $error");
exit;
}
if ($filename) {
$abs_path = $filepath . DIRECTORY_SEPARATOR . $filename;
if (file_exists($abs_path) && is_readable($abs_path)) {
header($_SERVER['SERVER_PROTOCOL'].' 200 OK');
header("Content-Type: application/zip");
header("Content-Type: application/rar");
header("Content-Transfer-Encoding: Binary");
header('Content-length: ' . filesize($abs_path));
header('Content-disposition: attachment; filename=' . $filename);
readfile($abs_path);
} else {
header("Location: $error");
}
}
Upvotes: 1
Views: 972
Reputation:
SOLVED I added:
ob_start();
if ($filename) {
$abs_path = $filepath . DS . $filename;
if (file_exists($abs_path) && is_readable($abs_path)) {
header('Content-type: application/octet-stream');
header('Content-length: ' . filesize($abs_path));
header('Content-disposition: attachment; filename=' . $filename);
header('Content-transfer-encoding: binary');
while (ob_get_level()) {
ob_end_clean();
}
readfile($abs_path);
} else {
header("Location: $error");
}
}
Upvotes: 3