2014-06-26 37 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

您是否可以更新您的答案,並將需要的代碼複製到公用文件夾中? –