Php laravel更新功能不工作
我想将图像保存到数据库中Php laravel更新功能不工作,php,mysql,laravel,laravel-4,Php,Mysql,Laravel,Laravel 4,我想将图像保存到数据库中 $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()函数似乎对
$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()
函数似乎对我的数据库没有任何作用,因为图片字段总是NULL
,尽管$base64
有数据。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); ?>">
$admin=admin::find(25);
#如果使用刀片模板系统,请将变量传递给视图和它们:
图片)}}“>
#如果您不使用刀片,请这样做
图片);?>“>
有任何错误/异常吗?@André我在问题中已经说过,我已经尝试了->save()
,但是没有任何东西被保存到我的数据库中。您不应该将任何图像作为base64存储在数据库中。只需将img保存到您的资产文件夹并存储到img的路径。@CarlosGoce我更愿意保存图像本身。@CarlosGoce好的,这听起来是一个不错的解决方案,您知道如何在laravel中这样做吗?我的目标文件夹应该在哪里?在视野中?在应用程序的根目录的中,具体取决于。如果图像不是私有的,您可以将其保存在公用文件夹中,否则,请将其保存在存储文件夹中。您可以使用内置函数public_path(“所需/目的地/内部/公共/文件夹”)或storage_path(“所需/目的地/内部/存储/文件夹”)。请使用复制到公用文件夹所需的代码更新您的答案,好吗?我更新了答案。我建议你检查一下Laravel助手的功能谢谢你的更新,一切都很好。除了文件的扩展名。文件保存时没有扩展名。我复制了您的代码,只需将$filename更改为admin\u 20
+1,然后接受了答案