2016-05-16 129 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()'将其移动到公共目录中。在我的例子中,我也给它一个新的名字以避免冲突。 –