0

I want to save an image to the database

$img_data = file_get_contents(Input::file('imgInp'));
        $base64 = base64_encode($img_data);
        $admin = Admin::find(25);
        $admin->picture = $base64;
        $admin->update();
        echo $base64;

the ->update() function seems to not doing anything to my database because the picture field is always NULL though the $base64 has data. the column type in mysql database is longblob and I already tried to do ->save() but still nothing is being saved to the database. help please

11
  • Any errors/exceptions ? Commented Jun 26, 2014 at 13:35
  • @André I already stated in the question that I have tried ->save() but nothing is being saved to my database Commented Jun 26, 2014 at 13:35
  • You should not store any image as base64 in your database. Just save the img to your assets folder and store the path to the img. Commented Jun 26, 2014 at 13:37
  • @CarlosGoce I would prefer to save the image itself. Commented Jun 26, 2014 at 13:38
  • @CarlosGoce Okay that sounds a good solution, do you know how to do that in laravel? Commented Jun 26, 2014 at 13:39

1 Answer 1

2

The image is uploaded on a temporary folder so:

#Get the path to the uploaded img
$filePath = Input::file('imgInp')->getRealPath();
$fileName = Input::file('imgInp')->getClientOriginalName();
$extension = Input::file('imgInp')->getClientOriginalExtension();

#Lets add the extension to the file name
$fileNameWithExtension = $fileName . '.' . $extension;

#Create the folder img/uploaded on your public folder!
$destination = public_path('img/uploaded/' . $fileNameWithExtension);

#Copy the img to your desired destination
#copy ( $filePath, $destination );

#Instead of the copy function you can use this laravel function
Input::file('imgInp')->move($destination);

#Save on database the path to your img

$admin = Admin::find(25);
$admin->picture = $destination;
$admin->update();

I hope it works. Didn't tried it

Then if you want to show your image just do the following:

$admin = Admin::find(25);

#Pass the variable to your view and them, if you use blade templating system:
<img src="{{asset($admin->picture)}}">

#If you are not using blade do it this way
<img src="<?php echo asset($admin->picture); ?>">
Sign up to request clarification or add additional context in comments.

8 Comments

where should my destination folder be? in the view? in the app in the root?
It depends. If the images are not private you can save them on the public folder, otherwise, save them on the storage folder. You can use the built in functions public_path('desired/destination/inside/public/folder') or storage_path('desired/destination/inside/storage/folder').
could you update your answer please with the needful code to copy to a public folder?
I updated the answer. I suggest you to check the Laravel Helper functions laravel.com/docs/helpers
thanks for the update, everything is working fine. except the extension of the file. the file is saved without extension. I copied you code and just change the $filename to admin_20 +1 to you and accepted answer
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.