我想将图像保存到数据库
$img_data = file_get_contents(Input::file('imgInp'));
$base64 = base64_encode($img_data);
$admin = Admin::find(25);
$admin->picture = $base64;
$admin->update();
echo $base64;
->update()
函数似乎没有对我的数据库执行任何操作,因为尽管 $base64
图片字段始终为 NULL
有数据。 mysql 数据库中的列类型是 longblob
并且我已经尝试执行 ->save()
但仍然没有任何内容被保存到数据库中。请帮忙
最佳答案
图像上传到临时文件夹中,因此:
#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();
我希望它能起作用。没试过
然后,如果您想显示您的图像,只需执行以下操作:
$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); ?>">
关于php - laravel 更新功能不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24431988/