Laravel 5.2将图像上传到存储

时间:2016-05-06 17:45:39

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

我想问一下,如何让某人选择一张图片,然后点击上传,图片就会保存到我的项目中(例如:project / public / images)。谢谢:))

2 个答案:

答案 0 :(得分:2)

你可以这样做:

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

答案 1 :(得分:0)

否则你可以这样做

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