2016-05-16 124 views
0

我想上傳文件夾中的圖像,並在數據庫中插入其名稱。像這樣的C:\xampp\tmp\phpAA38.tmp存儲在數據庫中,而不是圖像名稱,並沒有上傳任何東西。圖像上傳和插入名稱到laravel中的數據庫

$imageName = $request->file('image'); 
    if($imageName!==null) 
    { 
     $imageName->getClientOriginalExtension(); 
      $imageName->move(
      base_path() . '/public/images/new', $imageName); 
    } 
    $store= Result::insert(['Name'=>$name,Image'=>$imageName]); 

我保證形式使用enctype="multipart/form-data"

回答

0

你在你的代碼的一些錯誤,接受文件。例如。

  • 你不保存或使用的文件擴展名
  • 你不移動文件正確

看是否有此效果更好:

$imageName = $request->file('image'); 
if($imageName!==null) 
{ 
    // get the extension 
    $extension = $imageName->getClientOriginalExtension(); 
    // create a new file name 
    $new_name = date('Y-m-d') . '-' . str_random(10) . '.' . $extension; 
    // move file to public/images/new and use $new_name 
    $imageName->move(public_path('images/new'), $imageName); 
} 
$store= Result::insert(['Name'=>$name,Image'=>$new_name]); 
+0

圖像名將插入新名稱,但圖像未上傳到目錄中。 – micky

+0

好的。它應該是'$ imageName-> move(public_path('images/new'),$ new_name);'? – micky

+0

圖像上傳到臨時目錄。你需要使用'move()'將其移動到公共目錄中。在我的例子中,我也給它一個新的名字以避免衝突。 –