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

时间:2018-06-23 03:19:06

标签: laravel

我正在使用以下代码存储上传的文件

<div class="container"><h2 class="text-center"><?php echo $title;?></h2>
<br>

<?php 
    $attributes = array('id'=>'search_form', 'class'=> 'form-horizontal');
    echo validation_errors("<p class='bg-danger'>");
    echo form_open('customers/customer_search', $attributes);
?>
<div class="text-center"><p>Search using part of first name, last name or company</p></div>
<div class="form-group">
    <?php $data = array('class' => 'form-control','name' => 'search');?>
    <div class="col-sm-4"></div>
    <div class="col-sm-4"><?php echo form_input($data);?></div>
</div>

<div class="form-group">
    <div class="text-center">
    <?php $data = array('class' => 'btn btn-primary','name' => 'submit','value' => 'Search Customer');
    echo form_submit($data); ?>
    </div>
</div>

<?php echo form_close();?>

<?php if ($this->input->post('search')):?>
    <table class="table table-hover table-bordered table-condensed">
    <tr class="table-header">
          <td><b>ID</b></td>
          <td><b>Regular</b></td>
          <td><b>Last Name</b></td>
          <td><b>First Name</b></td>
          <td><b>Additional Contact</b></td>
          <td><b>Company</b></td>
          <td><b>City</b></td>
          <td><b>Details</b></td>
    </tr>
    <?php foreach ($results as $object){ ?>
    <tr>
      <td><?php echo $object['idcustomers'];?></td>
      <td>
      <?php 
      if ($object['marina_reg'] == 'Yes'):echo $object['marina_reg'];endif;?>
      </td>
        <td><?php echo ucwords(strtolower($object['lname']));?></td>
        <td><?php echo ucwords(strtolower($object['fname']));?></td>
        <td><?php echo ucwords(strtolower($object['add_name']));?></td>
        <td><?php echo ucwords(strtolower($object['company']));?></td>
        <td><?php echo ucwords(strtolower($object['city']));?></td>
        <td><a href="<?php echo base_url();?>customers/customer_edit/<?php echo $object['idcustomers'];?>">Details</a></td>
     </tr>      
    <?php } ?>  
  </table>
<?php endif;?>
</div>

上传的文件存储在 storage / app / 2 / filename.jpg

我想向用户显示他上传的文件。我该怎么办?

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

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

希望我的观点清楚。

工作解决方案

display.blade.php

 $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()));

web.php

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

感谢:@Boghani Chirag和@rkj

8 个答案:

答案 0 :(得分:3)

  

无法像您所说的那样公开访问文件,然后像这样读取文件

$userid = session()->get('user')->id;
$contents = Storage::get($userid.'/file.jpg'); 

假设您的文件位于路径storage/app/{$userid}/file.jpg     并且默认磁盘为local选中config/filesystems.php

  

可公开访问的文件

如果要使文件可公开访问,则将文件存储在此storage/app/public文件夹中。您可以在其中创建子文件夹并上传到该文件夹​​。将文件存储在storage/app/public中后,您只需要创建一个符号链接,laravel就会使用artisan命令。

php artisan storage:link

这将创建从storage/app/publicpublic/storage的符号链接。意味着现在您可以像这样访问文件

$contents = Storage::disk('public')->get('file.jpg'); 

此处文件物理路径位于storage/app/public/file.jpg,并且通过符号链接路径public/storage/file.jpg进行访问

假设您有子文件夹 storage/app/public/uploads,您可以在其中存储上传的文件,然后可以像这样

对其进行访问
$contents = Storage::disk('public')->get('uploads/file.jpg');
  

在公共文件夹中上传时,您可以在视图中访问它

echo asset('storage/file.jpg'); //without subfolder uploads

echo asset('storage/uploads/file.jpg');

检查详细信息https://laravel.com/docs/5.6/filesystem#configuration

答案 1 :(得分:2)

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('media/{filename}', [\App\Http\Controllers\MediaController::class, 'show']);

答案 2 :(得分:1)

尝试一下:

Storage::disk('your_disk_name')->getDriver()->getAdapter()->applyPathPrefix('your_file_name');

祝你好运!

答案 3 :(得分:1)

请您尝试以下代码

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) }}" />

谢谢

答案 4 :(得分:0)

可以请你试试

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

答案 5 :(得分:0)

enter image description here

请记住将文件夹放入存储/应用/公共/

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

php artisan storage:link

如果要访问2个文件夹的个人资料图片,请在刀片文件中这样做

<img src="{{ asset('storage/2/images/'.$user->profile_image) }}" />

答案 6 :(得分:0)

像这样上传

$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) }}" />

答案 7 :(得分:0)

创建路线:

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="">