[英]Download file from “protected” directory
如何從“受保護”目錄中下載文件以防止通過.htaccess直接訪問?
因此該文件位於/var/www/web1/domain.com/protecteddirectory/file.png
我想使用一個PHP腳本來檢查是否允許用戶下載(已完成)並在此之后啟動下載。
到目前為止,這是我的消息來源:
public function downloadAttachment()
{
$id = JRequest::getInt('id');
$user = JFactory::getUser();
$model = $this->getModel('Attachment');
$item = $model->getAttachment($id);
if(!$user->guest)
{
echo "<p><strong>Check if you are allowed to download...</strong></p>";
if($user->get('isRoot'))
{
echo "<p><strong>Preparing...</strong></p>";
$filename = $item->filename; //Name to display
$filepath = $item->filepath;
// start download now...
}
}
else
{
echo "<p><strong>You are not allowed to download this file...</strong></p>";
}
}
我該如何解決? 最好是自動檢測文件類型,因為可以下載png,zip,txt,doc,pdf等。
我可以使用以下代碼解決此問題:
$size = filesize($file);
header ( 'Content-Description: File Transfer' );
header("Content-Type: application/force-download");
header ( 'Content-Type: application/octet-stream' );
header ( 'Content-Disposition: attachment; filename=' . basename ( $file ) );
header ( 'Expires: 0' );
header ( 'Cache-Control: must-revalidate' );
header ( 'Pragma: public' );
header ( 'Content-Length: ' . filesize ( $file ) );
ob_clean();
flush();
readfile ( $file );
exit();
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.