0

我是 laravel 4.2 的新手我正在开发一个 Web 应用程序..现在我想将个人资料图片存储到数据库中。

这是我的控制器文件代码

public function profilepicture(){


 $data = Input::hasfile('file');

 DB::table('company')->insert(array('data'=>$data));
 return Redirect::to('/companyreg');
}

这是来自路由文件

Route::any('/company_profile','CompanyController@profilepicture');

这是表单视图

<form method="post"  action="{{url('/company_profile')}}">
<input type="file" name="file" />
<input type="submit" name="submitfile" value="upload" />
</form>
4

1 回答 1

1

首先将此添加到您的表单标签 files="true" enctype="multipart/form-data"

要上传文件,请查看以下代码:

 if (Input::file('file')->isValid()) {

      $destinationPath = 'uploads'; // upload path
      $extension = Input::file('file')->getClientOriginalExtension(); // getting image extension
      $fileName = rand(11111,99999).'.'.$extension; // renameing image
      Input::file('file')->move($destinationPath, $fileName); // uploading file to given path
      // sending back with message
      Session::flash('success', 'Upload successfully'); 

      DB::table('company')->insert(array('data'=>$fileName));
      return Redirect::back();
}
else {

  // sending back with error message.
  Session::flash('error', 'uploaded file is not valid');
  return Redirect::back();
}

因此,将图像名称或 base64 编码字符串存储到数据库,然后在任何您想要的地方显示该图像。

于 2016-06-23T07:06:03.553 回答