php - 如何检索laravel5.4存储文件夹中存储的图像?

标签 php mysql laravel

Storage/app/public-->这是我的图片存储路径。

Controller 代码:

public function addDeviceCategory(Request $cform)
{

if (Input::hasFile('imagefile'))
    {
    $name  = $this->getImageId().".".$cform->file('imagefile')-
 >getClientOriginalExtension();   

    $image = $cform->file('imagefile');        
    $resize = Image::make($image)->resize(700, 300)->encode($cform-
 >file('imagefile')->getClientOriginalExtension());
    $hash = md5($resize->__toString());

    $path = "public/" . $name;
    Storage::put($path, $resize->__toString());

    }
  $validation1=deviceCategory::select('deviceCategoryName')-
 >where("deviceCategoryName",$cform->deviceCategory)->first();

    if(is_null($validation1))
    {

    $temp=new deviceCategory;

    $temp->deviceCategoryId=$this->getDeviceCategoryId();
    $temp->deviceCategoryName=$cform->input('deviceCategory');
    $temp->subCategoryId=$cform->input('subCategory');
    $temp->image=$name;

    $temp->save();

    return redirect('formAddDeviceCategory');
   }

}

public function getImageId(){
  return md5(uniqid().microtime());
}

这就是我将图像存储到laravel存储路径(即Storage/app/public)的方式。这里存储成功。

查看代码:

 @php 
            $l=1
            @endphp
            @foreach($cdetails as $cdetail)
            <tr>
              <td>{{$l++}}</td>
              <td>{{$cdetail->deviceCategoryId}}</td>
              <td>{{$cdetail->categoryName}}</td>
              <td>{{$cdetail->subCategoryName}}</td>
              <td>{{$cdetail->deviceCategoryName}}</td>

          **<td><img src="{{ asset('storage/app/public/$cdetail->
              image') }}"  width="50" height="50"></img></td>**
             @endforeach

这里我尝试显示存储的图像。但那里没有显示任何内容。

最佳答案

首先确保您已将 storage/app/public 链接到 public/storage

如果没有运行php artisan storage:link

然后在您的 View 中使用 Storage::url('public/' . $cdetail->image) 获取链接。

关于php - 如何检索laravel5.4存储文件夹中存储的图像?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45628823/

相关文章:

php - 在自己的网站上使用 jQuery Themeroller?

php - jquery实时更新数据库中的行

laravel - vagrant laravel 第一次运行出错

laravel - 默认站点中的 SSL laravel forge

javascript - 无法弄清楚如何使用类名 cboxPhoto 获取特定 img 的 src(完整路径)

PHP 摆脱 'if' 条件并继续下一部分

mysql - 每 n 秒自动从数据库中删除过时的行

mysql - 使用单个查询查看所有表中的集体数据

mysql - 高效的数据库设计继承

mysql - Laravel:查询返回错误结果