user3305675
user3305675

Reputation: 9

Images upload form laravel

My image upload doesn't work:

Controller:

if (Input::hasFile('image')) {
        $bikecreate->image = Input::file('image');
        $destinationPath = public_path().'/upload/';
        $filename = str_random(6) . '_' . $bikecreate->users_id ;
        Input::file('image')->move($destinationPath, $filename);
    }

Form:

{{ Form::file('image', array('files' => true)) }}

After accepting form everything looks ok, but after the end of upload, filepath in database show .tmp/file at my server.

Upvotes: 0

Views: 186

Answers (1)

alexrussell
alexrussell

Reputation: 14202

Without seeing the rest of your code it's hard to see exactly what's going on but my guess is that your line $bikecreate->image = Input::file('image') is where you're setting the file's path for the database. You've actually set the UploadedFile instance as the image property on $bikecreate there, which, presumably, when serialised to something to put into the database gets __toString() called on it.

__toString() called on a File instance (which itself inherits __toString from SPLFileInfo returns the path to that file. So you'd think you're get the uploaded filename, but actually because an uploaded file is actually a temporary file in PHP, you get the temporary name.

Try changing that line to the following:

$bikecreate->image = Input::file('image')->getClientOriginalName();

This retrieves the actual original name of the uploaded file, not the temporary path given to it by PHP.

It goes without saying that this is only pertinent to UploadedFiles, normal files should just be able to be __toStringed to get the path to the file, although you'll notice that it would be the full path and not the basename. To get that, use getBaseName().

Upvotes: 1

Related Questions