2016-06-22 4 views
0

私はlaravel 4.2の新機能Webアプリケーションを開発中です。プロフィールの画像をデータベースに保存したいと思います。laravel 4.2 profilce画像の保存/アップロードとデータベースからの検索

これは私のコントローラファイルのコードである

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

まず第一に、あなたのformタグにこれを追加です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'のようなバリデータルールを使うことができます。 –

関連する問題