Laravel 4.2 Storing image path to database

拟墨画扇 提交于 2020-01-06 08:49:15

问题


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

回答1:


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;



回答2:


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.




回答3:


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

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

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");

When uploading, you can use

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

And when you need URL, you just can call

echo $storage_url.$name;

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




回答4:


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



回答5:


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

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



回答6:


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

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


来源:https://stackoverflow.com/questions/25993123/laravel-4-2-storing-image-path-to-database

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!