Laravel:如何访问本地磁盘中的文件?- 文件不存在 [英] Laravel: how to access a file in the local disk? - File does not exist
问题描述
在laravel中上传后,我正在尝试编辑一个excel文件.该文件已上传到本地磁盘,因此没有人可以从公众访问它.
I'm trying to edit an excel file after uploading it in laravel. the file is uploaded to local disk, so no one can access it from the public.
Filesystems.php
'local' => [
'driver' => 'local',
'root' => storage_path('app')
Routes.php
Route::get('test',function()
{
// Create new PHPExcel object
$objPHPExcel = new PHPExcel();
$objPHPExcel = PHPExcel_IOFactory::load(Storage::disk('local')->url('margin/analyser/0IGkQQgmGXkHrV9ISnBTrGyZFUPfjz3cWJbLGbDN.xlsx'));
$objPHPExcel->setActiveSheetIndex(0);
echo $objPHPExcel->getActiveSheet()->getHighestRow();
})->middleware('admin');
我不断收到错误消息:
Could not open /storage/margin/analyser/0IGkQQgmGXkHrV9ISnBTrGyZFUPfjz3cWJbLGbDN.xlsx for reading! File does not exist.
这有效
$objPHPExcel = PHPExcel_IOFactory::load('..'.Storage::disk('local')->url('app/margin/analyser/0IGkQQgmGXkHrV9ISnBTrGyZFUPfjz3cWJbLGbDN.xlsx'));
但是很烦人.我以为在filesystems.php中指定'root'=> storage_path('app')意味着Storage :: disk('local')将直接在应用程序中
but it is annoying. I thought specifying 'root' => storage_path('app') in the filesystems.php means that Storage::disk('local') will be in the app directly
推荐答案
URL方法用于获取文件的URL(即从浏览器访问文件的内容).您要使用 get()
方法的ID.
The URL method is used to get URL for the file (ie: something to access it from the browser). What you want to use id the get()
method.
$contents = Storage::get(''margin/analyser/0IGkQQgmGXkHrV9ISnBTrGyZFUPfjz3cWJbLGbDN.xlsx'');
话虽这么说,使用Laravel操纵上传的文件还有更简便的方法.
That being said, there is still easier way to manipulate uploaded files with Laravel.
// this will take the uploaded file from the request
// and store it into `/storage/someFolder`
$path = $request->file('file')->store('someFolder');
然后您可以使用返回到 $ path
的值来访问文件.
You can then use the value returned to $path
to access the file.
编辑
经过下面的讨论,OP决定,即使这不是完美的解决方案,他也会使用 $ objPHPExcel = PHPExcel_IOFactory :: load('..'.Storage :: disk('local')-> url($ fileLocation));
.
After discussion below, it was decided by OP that, even if it's not the the perfect solution, he would use $objPHPExcel = PHPExcel_IOFactory::load('..'.Storage::disk('local')->url($fileLocation));
.
这篇关于Laravel:如何访问本地磁盘中的文件?- 文件不存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!