2014-06-26 36 views
0

我想要的图像保存到数据库laravel更新功能无法正常工作

$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(),但仍然没有任何内容保存到数据库中。请帮助

+0

任何错误/异常? –

+0

@André我已经在问题中说过我已经尝试了' - > save()',但没有任何内容保存到我的数据库中 –

+0

您不应该在数据库中存储任何作为base64的图像。只需将img保存到资产文件夹并将路径存储到img即可。 –

回答

2

图片上的临时文件夹,以便上传:

#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); ?>"> 
+0

我的'目标文件夹'应该在哪里?在视图中?在'root'的'app'中? –

+0

这取决于。如果图像不是私密的,您可以将它们保存在公用文件夹中,否则将它们保存在存储文件夹中。您可以使用内置函数public_path('desired/destination/inside/public/folder')或storage_path('desired/destination/inside/storage/folder')。 –

+0

您是否可以更新您的答案,并将需要的代码复制到公用文件夹中? –