laravel 上传图片并保存图片路径到数据库 Lavavel 5.4

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46139463/
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-14 16:37:57  来源:igfitidea点击:

Upload Image and save image path to database Lavavel 5.4

laravel

提问by universal

I am creating a new post on Laravel. But I stuck here to upload an image and save the image path to database. My controller is

我正在 Laravel 上创建一个新帖子。但我坚持在这里上传图像并将图像路径保存到数据库。我的控制器是

if($request->hasFile('image'))
        {
            $image = $request->file('image');
            $filename = time() . '.'. $image->getClientOriginalExtension();

            $path = public_path('images');
            $imagepath = $request->image->move($path, $filename);
            $post->image = $imagepath;

        }

But it saves path as 'C:\xampp\tmp\php2CF9.tmp'. What is causing the error? Or are there any better approaches than this?

但它将路径保存为“C:\xampp\tmp\php2CF9.tmp”。导致错误的原因是什么?或者有没有比这更好的方法?

回答by gilganebu

Laravel can automaically save the correct path for you. First you need to setup the local file storage, if you haven't done so (https://laravel.com/docs/5.4/filesystem#configuration)

Laravel 可以自动为你保存正确的路径。首先你需要设置本地文件存储,如果你还没有这样做(https://laravel.com/docs/5.4/filesystem#configuration

Then you can do fancy stuff like:

然后你可以做一些花哨的事情,比如:

$post = new Post();
$post->image = $request->file('image')->store('image');

回答by Sreejith BS

1) Make sure you have added enctype="multipart/form-data"in your form and a <input type="file">with field name="image"

1) 确保你已经enctype="multipart/form-data"在你的表单中添加了一个<input type="file">with 字段name="image"

2) Create a new folder named imagesin your laravel publicfolder.

2)images在你的laravelpublic文件夹中创建一个新文件夹。

3) In your controller

3) 在你的 controller

if( $request->hasFile('image')) {
    $image = $request->file('image');
    $path = public_path(). '/images/';
    $filename = time() . '.' . $image->getClientOriginalExtension();
    $image->move($path, $filename);

    $post->image = $path;
}


$post->save();

4) In your view

4) 在你的 view

<img src="{{ asset('images/FILENAME.EXTENSION') }}">

Hope it's helpful.

希望它有帮助。

回答by RamAnji

$imageName = time().'.'.$request->image->getClientOriginalExtension();
$request->image->move(public_path('images'), $imageName);
store into database with file name
$post->image    = $imageName;
display in view
{{ URL::to('/images/'.$post->image) }}

回答by qqmydarling

You can try this. Whats the point of saving path to DB? U can access to your images by having filename and his extension

你可以试试这个。将路径保存到数据库有什么意义?你可以通过文件名和他的扩展名访问你的图像

if($request->hasFile('image'))
{
    $fileNameExt = $request->file('image')->getClientOriginalName();
    $fileName = pathinfo($fileNameExt, PATHINFO_FILENAME);
    $fileExt = $request->file('image')->getClientOriginalExtension();
    $fileNameToStore = $fileName.'_'.time().'.'.$fileExt;
    $pathToStore = $request->file('image')->storeAs('public/images',$fileNameToStore);
}

$post = new Post;
if($request->hasFile('image')){
            $post->img = $fileNameToStore;
        }
$post->save();

But first u have to create a link with artisan:

但首先你必须创建一个与工匠的链接:

php artisan storage:link

So your images will be stored in: ProjectName\storage\app\public\images

所以你的图像将存储在:ProjectName\storage\app\public\images

回答by Gammer

You can try, It will also store the full path with file name:

您可以尝试,它还会存储带有文件名的完整路径:

if ($request->hasFile('image')) {
    $filename = $request->file('image')->getClientOriginalName();
    $filename = url("/images") .
        "/" . uniqid() .
    $request->file('image')
        ->getClientOriginalName();
            $destinationPath = "images";
    $request->file('image')->move($destinationPath, $filename);
    $post->image = $filename;
        }