Laravel 4.2 Saving Database Path

I am having trouble maintaining the correct path for uploading a file 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 store 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
0
source share
6 answers

You can also change this line:

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

in

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

You can simply 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 also want to keep the leading slash.

0
source

, getRealPath, whitch .

URL- .

:

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

( : "1411468959-Bia.jpg" );

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

URL-,

echo $storage_url.$name;

, URL- . - , .

0

? .

- :

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

. :

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

:

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

, Laravel asset() Helper URL- :

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

Instead of storing the database path, save only the image name with a unique identifier. This is the best approach that works great.

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

Source: https://habr.com/ru/post/1613431/


All Articles