Jensej
Jensej

Reputation: 1305

Upload image laravel

How should it look using Laravel? view, controller, model, route for upload image? I have something like this, but it doesn't work:

public function postUpload()
{
   $file = Input::file('file');
   $filename = $file->getClientOriginalName();
   $path = 'public/uploads';
   return $file->move($path, $filename);
}

View:

{{Form::open(array('url' => 'foto/upload'))}}
{{Form::file('image')}}
{{Form::submit('Upload')}}
 {{Form::close()}}

Errors:

Call to a member function getClientOriginalName() on a non-object

Upvotes: 2

Views: 2459

Answers (3)

Mahmoud Zalt
Mahmoud Zalt

Reputation: 31130

Route:

$router->post('/add_rewards', ['as' => 'add_reward',
    'uses' => 'Controller@addReward',
]);

View to upload the Image:

1) add [ enctype="multipart/form-data” ] to the HTML form. example:

<form class="form-horizontal" action="{{route('add_reward')}}" method="post" enctype="multipart/form-data">

2) add the image file upload [ type=file ] example:

<div class="form-group">
    <div class="col-xs-12">
        <div class="form-material form-material-primary floating input-group">
            <input class="form-control btn-file" type="file" id="reward-image" name="reward-image">
            <span class="input-group-addon"><i class="fa fa-file-excel-o"></i></span>
        </div>
    </div>
</div>

Controller:

// check if file was uploaded
if ($request->hasFile('reward-image')) {
    // get the file object
    $file = $request->file('reward-image');
    // set the upload path (starting form the public path)
    $rewardsUploadPath = '/uploads/rewards/images/';
    // create a unique name for this file
    $fileName = str_replace([' ', ':'], '-', Carbon::now()->toDateTimeString())
        . '-' . str_random(5) . '.' . $file->getClientOriginalExtension();
    // move the uploaded file to its destination
    $file->move(public_path() . $rewardsUploadPath, $fileName);
    // save the file path and name
    $filePathAndName = $rewardsUploadPath . $fileName;
}

Save the Image on the object:

$reward = new Reward();

$reward->image = $image;
$reward->save();

View to display the image:

<td class="text-center">
    <img src="{{$reward->image}}"  width="40px;"/>
</td>

Upvotes: 1

Khan Shahrukh
Khan Shahrukh

Reputation: 6361

your controller should be

$filename = "";
$extension = "";
//check if your form sent some data
if (Input::hasFile('image')) {
// list of extensions allowed to be uploaded
$allowedext = array("png", "jpg", "jpeg", "gif");
//store the file received from the form in a var 
$photo = Input::file('image');
//set destination path where you will store your photo 
$destinationPath = public_path() . '/uploads';
//generate random filename 
$filename = str_random(12);
//get the extension of the file uploaded by the user
$extension = $photo->getClientOriginalExtension();
//validate if the user supplied file's extension matches allowed extension
if (in_array($extension, $allowedext)) {
//if every thing goes fine move the file 
$upload_success = Input::file('photo')->move($destinationPath, $filename . '.' . $extension);
                }

in your form :

{{Form::open(array('url' => 'foto/upload', 'files' => true))}}
{{Form::file('image')}}
{{Form::submit('Upload')}}
 {{Form::close()}}

Upvotes: 0

Joel Hinz
Joel Hinz

Reputation: 25384

At a quick glance, it looks mostly good. However, the HTML form needs to know that you want to send files. Change the form opening to this:

{{Form::open(array('url' => 'foto/upload', 'files' => true))}}

Also, you call the file image in the form, but then you call it file when you receive it. Change to this:

$file = Input::file('image');

Hopefully, that's enough to make it all work!

Upvotes: 1

Related Questions