我有一个Laravel Web应用程序,允许人们将他们的文件上传到uploads
目录。
我想允许任何文件类型和名称但不允许执行它们(只是静态文件上传/下载)所以我创建了一个.htaccess
文件:
/public_html/uploads/.htaccess:
php_flag engine off
但是当我尝试访问存储的文件(例如/uploads/photo.jpg)时,Laravel提出了NotFoundHttpException
。
如果我在行之前放置#
,我可以访问文件,但然后执行php脚本。
这是Laravel的htaccess文件
/public_html/uploads/.htaccess:
<IfModule mod_rewrite.c>
<IfModule mod_negotiation.c>
Options -MultiViews
</IfModule>
RewriteEngine On
# Redirect Trailing Slashes If Not A Folder...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)/$ /$1 [L,R=301]
# Handle Front Controller...
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [L]
# Handle Authorization Header
RewriteCond %{HTTP:Authorization} .
RewriteRule .* - [E=HTTP_AUTHORIZATION:%{HTTP:Authorization}]
</IfModule>
答案 0 :(得分:0)
您可以使用file_get_contents($filename)
获取文件内容,然后使用application\octect-stream
标题将文件作为直接静态下载发送。
$fileContent = file_get_contents($fullPathToFile);
header('Content-Type: application/octet-stream');
echo $fileContent;
使用Laravel,您始终可以使用内置方法:
https://laravel.com/docs/master/responses#file-downloads
供参考:
$name = 'theFileName.txt';
// You can override the name of the downloaded file
$headers = ['Content-Type'=>'application/octet-stream',];
// Sends a header saying the type is binary and should be accepted as such
// Fires off PSR-7 compatible response from Laravel
return response()->download($pathToFile, $name, $headers);
我希望这有帮助!