简体   繁体   中英

Image file not properly stored in Mysql database with Laravel

Here i try to store the user's image in the db when they upload it in the form , When they upload it the image is getting stored in the databse but in the form of BIN file which size is just 7 Bytes or 14 bytes and in the db it looks like [BLOB-14B] and [BLOB-7B]. But when i checked by directly uploading it on the database it worked pretty fine. What is the cause of the issue here

Here is the code of the form to get users image

<div class="form-group">
<label class="col-md-4 control-label">Upload image</label>

     <div class="col-md-6">
         <input type="file" class="form-control" name="image" id="image">


     </div>
</div>

I am not including the whole form code here and have included only the code that gets image file.

Here is the controller function code

 public function prof_details(Request $request)
    {
        $post = $request->all();

        $val=\Validator::make($request->all(),

        [
           'firstname' =>    'required',
           'lastname' =>     'required',
           'username' =>     'required',
           'phone'=>         'required',
           'nationality' =>  'required',
           'dobmonth' =>     'required',
           'dobyear' =>      'required',
           'dobday' =>       'required',
           'image' =>        'required',

        ]


        );

    if ($val ->fails()) 
    {

        return redirect()->back()->withErrors($val->errors());

    }

    else
    {
        $data = array(

            'firstname' => $post['firstname'] ,
           'lastname' => $post['lastname'],
           'username' => $post['username'],
           'phone' => $post['phone'],
           'nationality' => $post['nationality'],
           'dobmonth' => $post['dobmonth'],
           'dobyear' => $post['dobyear'],
           'dobday' => $post['dobday'],
           'image' =>$post['image'],



            );

        $updatedata = DB::table('users')->where('name',\Auth::user()->name)
        ->update($data);



        if ($updatedata>0) {


            return redirect('home');
    }
    else
    {
        return "something";
    }


    }
}
<input type="file" class="form-control" name="image" id="image">

Files are not stored in the $_POST variable but instead are stored in a temp file. You can obtain data on this file from the $_FILES variable 1 [2].

So, your uploaded file information would be in $_POST['image']. In order to obtain the actual image you would need to read the temp file that is stored in $_POST['image']['tmp_name'].

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM