【问题标题】:laravel 4.2 storing/uploading profilce picture and retrieving from databaselaravel 4.2 存储/上传个人资料图片并从数据库中检索
【发布时间】:2016-10-24 11:00:48
【问题描述】:

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

【问题讨论】:

  • 你有什么问题?

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


【解决方案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 编码字符串存储到数据库中,然后在您想要的任何位置显示该图像。

【讨论】:

  • @MohitArya 您可以使用验证器规则,例如'required|image|mimes:png,jpg,jpeg,gif,bmp',
猜你喜欢
  • 2021-01-15
  • 1970-01-01
  • 1970-01-01
  • 2019-10-17
  • 2012-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多