2016-06-22 9 views
0

i m new to laravel 4.2 Я разрабатываю веб-приложение. Теперь я хочу сохранить изображение профиля в базе данных.laravel 4.2 Сохранение/загрузка изображения профилей и извлечение из базы данных

это мой контроллер код

public function profilepicture(){ 


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

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

файл это от маршрута файла

Route::any('/company_profile','[email protected]'); 

это представление формы

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

В чем проблема? – TheFallen

ответ

0

Прежде всего добавить к вашей форме тега 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 строки в базу данных, а затем показать, что изображение, куда вы хотите.

+0

Но что, если я хочу разрешить пользователю загружать определенный формат файла ??? –

+0

@MohitArya вы можете использовать правило валидатора как '' required | image | mimes: png, jpg, jpeg, gif, bmp ', ' –