Laravel 4.2 将图片路径存储到数据库

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

Laravel 4.2 Storing image path to database

phpimagelaravellaravel-4

提问by Fede

I'm having trouble to store the correct image path of a file upload to the database.

我无法将上传到数据库的文件的正确图像路径存储起来。

this is my code:

这是我的代码:

public function store()
    {
        $input = Input::all();

        try
        {
            $this->modelForm->validate($input);

            if ( Input::hasFile('thumbnail')) {

                $file = Input::file('thumbnail');
                $name = time().'-'.$file->getClientOriginalName();
                $file = $file->move('uploads/', $name);
                $input['file'] = $file->getRealPath();
            }

            $model = new Model($input);
            $model->save();

            Redirect::back();

        }
        catch (FormValidationException $e)
        {
            return Redirect::back()->withInput()->withErrors($e->getErrors());
        }
    }

so... this is what i'm storing with this code:

所以......这是我用这段代码存储的内容:

/home/vagrant/code/website/public/uploads/1411468959-Bia.jpg

and this is what I need to store i think:

这就是我需要存储的东西,我认为:

public/uploads/1411468959-Bia.jpg

采纳答案by Marcin Nabia?ek

You can also change this line:

您还可以更改此行:

$input['file'] = $file->getRealPath();

into:

进入:

$path = $file->getRealPath();
$pos = strpos($path,'/public/');
if ($pos !== false) {
    $path = substr($path, $pos + 1);
}
$input['file'] = $path;

回答by lowerends

You could just use this:

你可以用这个:

public function store()
{
    $input = Input::all();

    try
    {
        $this->modelForm->validate($input);

        if ( Input::hasFile('thumbnail')) {

            $file = Input::file('thumbnail');
            $name = time().'-'.$file->getClientOriginalName();
            $file = $file->move('uploads/', $name);
            $input['file'] = '/public/uploads/'.$name;
        }

        $model = new Model($input);
        $model->save();

        Redirect::back();

    }
    catch (FormValidationException $e)
    {
        return Redirect::back()->withInput()->withErrors($e->getErrors());
    }
}

Note that you probably want to store the leading slash as well.

请注意,您可能还想存储前导斜杠。

回答by Karolis Ti?ka

Well, you use getRealPath, whitch return absolute path to the file.

好吧,您使用 getRealPath,它返回文件的绝对路径。

I'l prefer to set storage path and storage url to your settings.

我更喜欢将存储路径和存储 url 设置为您的设置。

For example:

例如:

$storage_path = '/home/vagrant/code/website/public/uploads/';
$storage_url  = 'http://storage.yourwebsite.com/uploads/';

Then just save file basename to the database (in your case: "1411468959-Bia.jpg");

然后只需将文件 basename 保存到数据库中(在您的情况下:“1411468959-Bia.jpg”);

When uploading, you can use

上传时,您可以使用

$file->move($storage_path, $name);

And when you need URL, you just can call

当您需要 URL 时,您只需调用

echo $storage_url.$name;

In this way your storage path and url stays flexible. And filename in database is everything you need.

通过这种方式,您的存储路径和 url 保持灵活。数据库中的文件名就是您需要的一切。

回答by Thomas Jensen

Do you actually need to store the path in the database? You can just store the image name and generate the path in a view presenter or the model.

你真的需要在数据库中存储路径吗?您可以只存储图像名称并在视图展示器或模型中生成路径。

Something like:

就像是:

public function getPathAttribute()
{
    return 'public/uploads/' . $this->fileName);
}

Or better yet store the upload path as a config variable. This will make it a lot easier if you move you upload folder in the future:

或者更好地将上传路径存储为配置变量。如果您将来移动上传文件夹,这将使事情变得容易得多:

public function originalUrl()
{
    return Config::get('assets.upload') . $this->fileName);
}

To move the file to the defined upload path:

将文件移动到定义的上传路径:

$file = $file->move(Config::get('assets.upload'), $name);

回答by Luca

a bit late to the party, but Laravel has a useful asset()Helperto generate an asset public url:

聚会有点晚了,但 Laravel 有一个有用的asset()Helper来生成资产公共 url:

$input[ 'file' ] = asset( '/uploads/' . $this->fileName );

回答by Rizwan Mughal

Instead of storing image path to database, store only image name with unique id.This is the best approach an works fine.

不存储图像路径到数据库,只存储具有唯一 id 的图像名称。这是最好的方法,效果很好。

    $destinationPath = 'wisdom/company_images/logo/';

    if(Input::Hasfile('image')){
        $files = Input::file('image');
        $filename = md5(uniqid()).'.'.$files->getClientOriginalExtension();

        $ext = explode('.',$filename);
        $ext = strtolower($ext[count($ext)-1]);
        $upload_success = $files->move($destinationPath, $filename);
    }else{
        $filename = Input::get('upload_image','');
    }