【问题标题】:Laravel 5.2 upload image to storageLaravel 5.2 上传图片到存储
【发布时间】:2016-09-01 21:19:18
【问题描述】:

我想问,我怎样才能让某人选择一张图片,然后他点击上传,图片将保存到我的项目中(例如:project/public/images)。谢谢你:)

【问题讨论】:

  • 你试过什么?展示您的一些努力,您在 Google 上打开了多少链接以及您在那里不理解的内容。这是一个非常简单和通用的任务,您应该通过简单的 Google 搜索来获得它。
  • 我已经尝试了很多东西,但仍然没有任何效果,我访问了至少 50 个论坛、文章和问题,但大多数都有关于如何将图像保存到数据库的教程跨度>

标签: laravel file-upload laravel-5 upload laravel-5.2


【解决方案1】:

你可以这样做:

$file = $request->file('image');
$name = $file->getClientOriginalName();
$path = public_path("images/$name");

Storage::put($path, File::get($file->getRealPath()));

但是。我建议使用 laravel medialibrary 包:

https://docs.spatie.be/laravel-medialibrary/v4/introduction

这样你就可以做到:

$post->addMediaFromRequest('image')->toCollection('images'); 

【讨论】:

  • 感谢您的推荐:)
【解决方案2】:

否则你可以这样做

    $destinationPath = '';
    $filename        = '';

    if (Input::hasFile('file')) {
        $file            = Input::file('file');
        $destinationPath = public_path().'/images/';
        $filename        = time() . '_' . $file->getClientOriginalName();
        $filename = str_replace(' ','_',$filename);
        $uploadSuccess   = $file->move($destinationPath, $filename);
    }

【讨论】:

    猜你喜欢
    • 2016-08-16
    • 2022-11-26
    • 2016-08-18
    • 2018-04-17
    • 2018-06-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-11-25
    相关资源
    最近更新 更多