laravel 4.2存储/上传profilce图片并从数据库中检索

时间:2016-06-22 13:11:37

标签: mysql laravel web laravel-routing laravel-4.2

我是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>

1 个答案:

答案 0 :(得分: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编码的字符串存储到数据库,然后在任何地方显示该图像。

相关问题