shoulieheinds's avatar

Uploading File / Photo.

I have a form Registration, where fields: name, dob, gender, religion, address, nophone, email, password and Photo.

Here I have RegisterController@create method to store data in database.

My Question: To uploading Photo, Am I must make new specify controller to uploading photo, or I can include together inside create method on RegisterController.

Please give me explanation for it. and if any example to do this, give me a solution. thanks so much ?

0 likes
8 replies
abusalameh's avatar

you can do both solutions but if you want to upload it with the form directly make sure to add

enctype="multipart/form-data"

to your form as an attribute , then in the controller check for the file like

$request->hasFile('fieldName')

and check this url to know more details about file uploads

https://laravel.com/docs/5.4/filesystem#file-uploads

MalaniDerrick's avatar

<?php namespace App\Http\Controllers;
use Input;
use Validator;
use Redirect;
use Request;
use Session;
class ApplyController extends Controller {
public function upload() {
  // getting all of the post data
  $file = array('image' => Input::file('image'));
  // setting up rules
  $rules = array('image' => 'required',); //mimes:jpeg,bmp,png and for max size max:10000
  // doing the validation, passing post data, rules and the messages
  $validator = Validator::make($file, $rules);
  if ($validator->fails()) {
    // send back to the page with the input data and errors
    return Redirect::to('upload')->withInput()->withErrors($validator);
  }
  else {
    // checking file is valid.
    if (Input::file('image')->isValid()) {
      $destinationPath = 'uploads'; // upload path
      $extension = Input::file('image')->getClientOriginalExtension(); // getting image extension
      $fileName = rand(11111,99999).'.'.$extension; // renameing image
      Input::file('image')->move($destinationPath, $fileName); // uploading file to given path
      // sending back with message
      Session::flash('success', 'Upload successfully'); 
      return Redirect::to('upload');
    }
    else {
      // sending back with error message.
      Session::flash('error', 'uploaded file is not valid');
      return Redirect::to('upload');
    }
  }
}
}

https://laracasts.com/discuss/channels/general-discussion/how-to-upload-file-and-store-to-database?page=1

@shoulieheinds hope you working let me know if not working.

Thanks

shoulieheinds's avatar

@MalaniDerrick .. To do this, am I must create new table for specify store file? ,So how to saving data together wwith other fields like name, pob, dob, address, no_phone, etc. ? I'm still confuse to upload file together with other fields...

vipin93's avatar

why u confused in simple word u will upload your photo name along with other stuff

shoulieheinds's avatar

Cause, I dont know how it's working... I want save data user identities and file photo. But here, I have two controller-> RegisterController & UploadController. how I can call PhotoController in RegisterController?

Please or to participate in this conversation.