zersiax's avatar

Some trouble with file uploading logic

Hi all,

I'm relatively new to Laravel and could really use a knowledgeable pair of eyes on this one. I have the following function:

public function postHandleCreateImage() { // Handle create form submission. $destinationPath= ''; $filename = ''; if (Input::hasFile('file_location')) { $file = Input::file('file_location'); $destinationPath= public_path() . 'img/'; $filename = str_random(6) . '_' . $file->getClientOriginalName(); $uploadSuccess = $file->move($destinationPath, $filename); }

        $image = new Image;

$image->title_nl = Input::get('title_nl'); $image->title_en = Input::get('title_en'); $image->description_nl = Input::get('description_nl'); $image->description_en = Input::get('description_en'); $image->alt_tag = Input::get('alt_tag'); $image->file_location = Input::get('file_location'); $image->location_id = Input::get('location_id'); $image->type = Input::get('type'); $image->save(); return Redirect::action('LocationController@index'); }

, but when it is caled, SQL reports file_location is null. So I am guessing something is going wrong when trying to get the file_location from the Input. what am I missing?

Thanks, Zersiax

0 likes
4 replies
tuurbo's avatar
tuurbo
Best Answer
Level 11

Shouldn't the file_location be the path that you moved the file too?

Maybe change

$image->file_location = Input::get('file_location');

to

$image->file_location = $destinationPath . $filename;

and if a file_location is required, then you should probably throw an error back to the user that submitted the form if something went wrong.

edited: fixed some stuff

tuurbo's avatar

There's also a problem your $destinationPath, its missing a leading forward slash.

It should be changed to...

public_path() . '/img/';
// or
public_path('img/');
zersiax's avatar

Thanks a billion :) This appears to work fine now. :) What I am noticing is that it is writing the full absolute path though. Won't this break things when the project is moved?

Regards, Zersiax

tuurbo's avatar

No problem. If all the images are stored in a single folder, then you could just save the $filename

Please or to participate in this conversation.