Laravel 将图像保存到公共文件夹?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/42623916/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-08 17:40:52  来源:igfitidea点击:

Laravel saving image to public folder?

imagelaravellaravel-5laravel-5.3

提问by Przemek

So I am trying to save uploaded image to laravel/public folder and return src so a path to where the file has been saved, however nothing happens after I choose an image and I get no errors, what might be wrong?

所以我试图将上传的图像保存到 laravel/public 文件夹并返回 src 以便保存文件的路径,但是在我选择图像后没有任何反应并且我没有收到任何错误,可能有什么问题?

public function testing(Request $request) {
    if(Input::file())
    {
        $image = Input::file('img');
        $filename = time() . '.' . $image->getClientOriginalExtension();
        $path = public_path('images/' . $filename);
        Image::make($image->getRealPath())->resize(200, 200)->save($path);
        $user->image = $filename;
        $user->save();
    }
}


<form action="{{ action('BuilderController@testing') }}" enctype="multipart/form-data" role="form" method="POST">
    <input id="img" class="form-control filestyle margin images" data-input="false" type="file" data-buttonText="Upload Logo" data-size="sm" data-badge="false" onchange="uploadImage();" />
</form>

回答by Sanzeeb Aryal

Try with move()method?

move()方法试试?

$filename = time().'.'.request()->img->getClientOriginalExtension();
request()->img->move(public_path('images'), $filename);

$user->image=$filename;
$user->save();

回答by Sagar Arora

You can easily upload image to public folder as following:

您可以轻松地将图像上传到公共文件夹,如下所示:

 $image = $request->file('image');

$input['imagename'] = time().'.'.$image->getClientOriginalExtension();

$destinationPath = public_path('/thumbnail');

$img = Image::make($image->getRealPath());

$img->resize(100, 100, function ($constraint) {

    $constraint->aspectRatio();

})->save($destinationPath.'/'.$input['imagename']);

/*After Resize Add this Code to Upload Image*/
$destinationPath = public_path('/');


$image->move($destinationPath, $input['imagename']);

And also you can check it here to resize and upload image:

您也可以在此处检查以调整大小和上传图像:

http://itsolutionstuff.com/post/laravel-5-image-upload-and-resize-example-using-intervention-image-packageexample.html

http://itsolutionstuff.com/post/laravel-5-image-upload-and-resize-example-using-intervention-image-packageexample.html

回答by kristi tanellari

you need to add name="img"on your form.

你需要name="img"在你的表格上添加。

回答by jeugen

First, you have not added file input name

首先,您没有添加文件输入名称

if(Input::file('inputname')){
//
}

Secondly,

其次,

For laravel-5.3 You can determine if a file is present on the request using the hasFile method

对于 laravel-5.3,您可以使用 hasFile 方法确定请求中是否存在文件

if ($request->hasFile('photo')) {
    //
}

And you can retrieve the uploaded file

您可以检索上传的文件

$file = $request->file('photo');

You can find the complete documentation here

您可以在此处找到完整的文档

https://laravel.com/docs/5.3/requests#files

https://laravel.com/docs/5.3/requests#files