【问题标题】:Upload and retrieve image in Laravel在 Laravel 中上传和检索图像
【发布时间】:2017-07-18 14:27:36
【问题描述】:

我需要为头像保存图片。

谁能给我一个简单的代码来保存和检索图像?

我需要:

  • 将图像保存在文件夹中
  • 在数据库中保存图像名称
  • 终于在图片标签上检索;我必须通过查询生成器来完成

表格:

 <form action="" method="post" role="form" multiple>
        {{csrf_field()}}
        <legend>Form Title</legend>

        <div class="form-group">
            <label for="">Your Image</label>
            <input type="file" name="avatar">
        </div>
        <button type="submit" class="btn btn-primary">save</button>
        <a href="{{'/'}}" class="btn btn-primary">back</a>
     </form>
            <img name="youravatar" src="">
        </div>

路线:

Route::get('pic','avatarController@picshow');

Route::post('pic','avatarController@pic');

控制器:

我有avatarController,但它是空的,因为我不知道该怎么做。

数据库:

表格名称:头像
字段:名称 id、imgsrc、created_at、Updated_at

其他:

我找到了这段代码,但我什么也找不到:

 if ($request->hasFile('avatar')) {
      $file = array('avatar' => Input::file('avatar'));
      $destinationPath = '/'; // upload path
      $extension = Input::file('avatar')->getClientOriginalExtension();
      $fileName = rand(11111,99999).'.'.$extension; // renaming image
      Input::file('avatar')->move($destinationPath, $fileName);
  }

【问题讨论】:

  • 到目前为止你有什么(控制器、表单、数据库结构)
  • 帖子已编辑。 tnx老兄
  • 尝试在
    标签内设置 enctype="multipart/form-data"
  • 是的,我看到了,但我几乎什么都做不了。

标签: mysql database image laravel file-upload


【解决方案1】:

首先,确保表单中有 encrypt 属性

<form action="#" method="post" enctype="multipart/form-data">

你可以在你的控制器中使用类似的东西

public function save(Request $request)
{

   $file = $request->file('file');
   // rename your file
   $name = $file->getClientOriginalName();
   \Storage::disk('local')->put($name,  \File::get($file));

   return "file saved";
}

是的,您也应该将文件路径存储在数据库中。

确保您为图像使用一致的路径,例如

最后你必须创建一个路由来让公共访问你的图像文件,像这样:

Route::get('images/{file}', function ($file) {

    $public_path = public_path();
    $url = $public_path . '/storage/' . $file;

    // file exists ?
    if (Storage::exists($archivo))
    {
       return response()->file($pathToFile);
    }

    //not found ? 
    abort(404);
});

查看有关Laravel Responses的文档

我希望这能让你知道该怎么做。

【讨论】:

  • tnx 兄弟,很有用
  • 伙计们再次需要帮助。请查收
【解决方案2】:

在 laravel 5.4 中上传图片

  1. 检查请求是否有图片

$request-&gt;hasFile('image')

$request-&gt;file('image')-&gt;isValid()

  1. 现在保存图片

$request->inputname->store('folder-name') return image path 'folder name/created image name $request->image->store('images')

  1. 检查图像是否存在

Storage::disk('local')-&gt;exists('image name');

  1. 删除图片

Storage::delete('image');

这是我的代码

if ($request->hasFile('image') && $request->file('image')->isValid())
            {
                $path = $request->image->store('images');
                if(!empty($path)){
                    $edit = Model::FindOrFail($id);
//                    Delete old image
                    $exists = Storage::disk('local')->exists($edit->image);
                    if($exists){
                        Storage::delete($edit->image);
                    }
                    $edit->image = $path;
                    $edit->save();
                }
            }

Reference

【讨论】:

    猜你喜欢
    • 2015-06-13
    • 1970-01-01
    • 2021-07-26
    • 1970-01-01
    • 2012-08-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-09-26
    相关资源
    最近更新 更多