Laravel 4.2 Storing image path to database

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

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

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

Method 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.

Method 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.

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

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

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x