Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/10.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Laravel从存储器中检索图像以查看_Laravel - Fatal编程技术网

Laravel从存储器中检索图像以查看

Laravel从存储器中检索图像以查看,laravel,Laravel,我使用下面的代码来存储上传的文件 $file = $request->file($file_attachment); $rules = []; $rules[$file_attachment] = 'required|mimes:jpeg|max:500'; $validator = Validator::make($request->all(), $rules); if ($validator->fails()

我使用下面的代码来存储上传的文件

 $file = $request->file($file_attachment);
        $rules = [];
        $rules[$file_attachment] = 'required|mimes:jpeg|max:500';
        $validator = Validator::make($request->all(), $rules);
        if ($validator->fails()) {
            return redirect()->back()
                ->with('uploadErrors', $validator->errors());
        }

        $userid = session()->get('user')->id;
        $destinationPath = config('app.filesDestinationPath') . '/' . $userid . '/';
        $uploaded = Storage::put($destinationPath . $file_attachment . '.' . $file->getClientOriginalExtension(), file_get_contents($file->getRealPath()));
上传的文件存储在存储/app/2/filename.jpg

我想向用户展示他上传的文件。我该怎么做

$storage=storage::get('/2/filename.jpg')

我收到了不可读的文本。我可以确认文件已被读取。但是如何将其作为图像显示给用户

希望我把我的观点说清楚

工作解决方案

display.blade.php

<img src="{{ URL::asset('storage/photo.jpg') }}" />

感谢:@Boghani Chirag和@rkj

你能试试这个吗

$storage = Storage::get(['type_column_name']);
试试这个:

Storage::disk('your_disk_name')->getDriver()->getAdapter()->applyPathPrefix('your_file_name');
祝你好运

文件不能像你说的那样公开访问然后像这样读取文件

$contents = Storage::disk('public')->get('file.jpg'); 
$uploadedFile = $request->file('photo');
$photo = "my-prefix" . "_" . time() . "." . $uploadedFile->getClientOriginalExtension();
$photoPath = \Illuminate\Support\Facades\Storage::disk('local')->putFileAs(
   "public/avatar",
   $uploadedFile,
   $photo
);
<img src="{{ asset('storage/avatar/'.$filename) }}" />
假设您的文件位于路径
storage/app/{$userid}/file.jpg
默认磁盘是
local
check
config/filesystems.php

可公开访问的文件

如果要使文件可公开访问,请将文件存储在此
存储/app/public
文件夹中。您可以在其中创建子文件夹并上传到那里。一旦您将文件存储在
存储/app/public
中,那么您只需创建一个符号链接,laravel就可以使用artisan命令来创建它

php artisan storage:link
这将创建一个
storage/app/public
public/storage
的符号链接。意味着现在你可以像这样访问你的文件

$contents = Storage::disk('public')->get('file.jpg'); 
$uploadedFile = $request->file('photo');
$photo = "my-prefix" . "_" . time() . "." . $uploadedFile->getClientOriginalExtension();
$photoPath = \Illuminate\Support\Facades\Storage::disk('local')->putFileAs(
   "public/avatar",
   $uploadedFile,
   $photo
);
<img src="{{ asset('storage/avatar/'.$filename) }}" />
这里的文件物理路径位于
storage/app/public/file.jpg,它通过符号链接路径
public/storage/file.jpg

假设您有子文件夹
存储/app/public/uploads
存储上传的文件,然后您可以像这样访问它

$contents = Storage::disk('public')->get('uploads/file.jpg');
当您在公用文件夹中进行上载时,您可以在视图中访问它


查看详细信息

您能试试这个代码吗

routes.php

Route::group(['middleware' => ['web']], function() {
    Route::get('storage/storage_inner_folder_fullpath/{filename}', function ($filename) {
        return Image::make(storage_path() . '/storage_inner_folder_fullpath/' . $filename)->response();
    });
});
查看文件代码

<img src="{{ URL::asset('storage/storage_inner_folder_fullpath/'.$filename) }}" />

谢谢

记住将文件夹放入存储器/app/public/

创建符号链接符号链接以访问此文件夹

php artisan storage:link
如果要访问2文件夹的配置文件图像,请在刀片文件中执行此操作

<img src="{{ asset('storage/2/images/'.$user->profile_image) }}" />
profile\u image)}}/>
像这样上传

$contents = Storage::disk('public')->get('file.jpg'); 
$uploadedFile = $request->file('photo');
$photo = "my-prefix" . "_" . time() . "." . $uploadedFile->getClientOriginalExtension();
$photoPath = \Illuminate\Support\Facades\Storage::disk('local')->putFileAs(
   "public/avatar",
   $uploadedFile,
   $photo
);
<img src="{{ asset('storage/avatar/'.$filename) }}" />
然后像这样进入

$contents = Storage::disk('public')->get('file.jpg'); 
$uploadedFile = $request->file('photo');
$photo = "my-prefix" . "_" . time() . "." . $uploadedFile->getClientOriginalExtension();
$photoPath = \Illuminate\Support\Facades\Storage::disk('local')->putFileAs(
   "public/avatar",
   $uploadedFile,
   $photo
);
<img src="{{ asset('storage/avatar/'.$filename) }}" />

Laravel 8

控制器

class MediaController extends Controller
{ 

    public function show(Request $request, $filename)
    {
        $folder_name = 'upload';

        $filename = 'example_img.jpeg';

        $path = $folder_name.'/'.$filename;

        if(!Storage::exists($path)){
            abort(404);
        }

        return Storage::response($path);
    }
}
路线


创建路由:

Route::get('image/{filename}', 'HomeController@displayImage')->name('image.displayImage');
public function displayImage($filename)

{

  

    $path = storage_public('images/' . $filename);

   

    if (!File::exists($path)) {

        abort(404);

    }

  

    $file = File::get($path);

    $type = File::mimeType($path);

  

    $response = Response::make($file, 200);

    $response->header("Content-Type", $type);

 

    return $response;

}

<img src="{{ route('image.displayImage',$article->image_name) }}" alt="" title="">
创建控制器方法:

Route::get('image/{filename}', 'HomeController@displayImage')->name('image.displayImage');
public function displayImage($filename)

{

  

    $path = storage_public('images/' . $filename);

   

    if (!File::exists($path)) {

        abort(404);

    }

  

    $file = File::get($path);

    $type = File::mimeType($path);

  

    $response = Response::make($file, 200);

    $response->header("Content-Type", $type);

 

    return $response;

}

<img src="{{ route('image.displayImage',$article->image_name) }}" alt="" title="">
public函数displayImage($filename)
{
$path=storage\u public('images/'。$filename);
如果(!File::exists($path)){
中止(404);
}
$file=file::get($path);
$type=File::mimeType($path);
$response=response::make($file,200);
$response->header(“内容类型”,$Type);
返回$response;
}
图像(名称)}}“alt=”“title=”“>

您是否尝试过从公用文件夹创建指向图像存储文件夹的符号链接?然后从那里访问url?(一些nginx/apache配置可能需要允许symlinked dir),虽然我不建议这样做,但尝试将其保存在公用文件夹中。就个人而言,我更喜欢在处理后将图像上传到cdn或像cloudinary这样的图像提供商,并从他们那里提供url。否。我不能那样做。用户信息更敏感,我无法将其放入公用文件夹或cloudinary中…我明白了,将其用作数据URI如何?使用img标记向用户提供服务:您可以将文件名存储在db中,然后您可以轻松找到在刀片文件中显示的路径什么是类型\u列\u名称您的表列名哪里是存储映像路径什么是磁盘\u名称这将帮助您了解什么是“映像”?您没有指定定义。