Reputation: 1179
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
Upvotes: 0
Views: 413
Reputation: 1665
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); ?>">
Upvotes: 2