guezandy's avatar

Image uploading Laravel 5!

Just trying to upload an image and move it into the local directory. Eventually trying to configure it to S3.

            <div class="content">
              <div class="title">Laravel 5</div>
                {!! Form::open(
                  array(
                      'route' => 'upload',
                      'class' => 'form',
                      'novalidate' => 'novalidate',
                      'files' => true)) !!}


              <div class="form-group">
                  {!! Form::label('Image') !!}
                  {!! Form::file('image', null) !!}
              </div>

              <div class="form-group">
                  {!! Form::submit('Upload Image') !!}
              </div>
              {!! Form::close() !!}
              </div>

Route.php

Route::any('upload', array('as' => 'upload', 'uses' => 'UploadController@upload'));

UploadController.php: Tried 3 different approaches!

    public function upload()
    {
    $imageName = str_random(3) . '.' .
    Input::file('image')->getClientOriginalExtension();

    /*ONE*/
    Input::file('image')->move(
        base_path() . '/public/images/', $imageName
    );
/*TWO*/
    Request::file('image')->move(
        base_path() . '/public/images/', $imageName
    );
/*THree */
    Storage::disk('local')->put('file.txt', 'Contents');

    return view('welcome');
    }
0 likes
10 replies
IsaacBen's avatar

Take a look at my controller, I get the images and store them at /public/files.

    public function store(Image $image,ImageRequest $request)
    {
        if(Input::get('no_email')){
            return 'Field must be empty';    -->This is just for "Honey Pot"
        }
        $filename = Input::file('filename');
        $change = $filename->getClientOriginalExtension();
        $newfilename = Auth::id().str_random(10).'.';
        $filename->move('files', "{$newfilename}" .$change);  
        $image->filename = 'files' . '/' ."{$newfilename}" .$change;
        $image->caption = Input::get('caption');
        $image->user_id = Auth::id();
        $image->user_name = Auth::user()->name;
        $image->save();
        return redirect('home');
      
    }
guezandy's avatar

Hey @itzikbenh thanks for the response, can I see your route.php for this.

Just trying to figure out where the Image and ImageRequest params came from.

Thanks!

1 like
IsaacBen's avatar

Route:

Route::post('home/store','HomeController@store');

Form:

{!! Form::open(array('files' => TRUE, 'action' => 'HomeController@store')) !!}

<div class="form-group">
  {!! Form::label('filename', 'My File') !!}
  {!! Form::file('filename') !!}
</div>
<div class="form-group">
    {!! Form::label('caption', 'Caption:')  !!}
    {!! Form::textarea('caption', null, ['class' => 'form-control','rows'=>'4']) !!}    
</div>
  {!! Form::text('no_email', '',array('style'=>'display:none','name'=>'winner')) !!}
<div class="form-group">
  {!! Form::submit('Upload photo') !!}
</div>
{!! Form::close() !!}
1 like
StuffedGoat's avatar

@itzikbenh May I ask you how that works with you Honey-Pot?

So you have a hidden field (display:none) and when ever a bot comes to your site and trys to input a spam e-mail address you are firing this message?

if(Input::get('no_email')){
    return 'Field must be empty';    -->This is just for "Honey Pot"
}
guezandy's avatar

@itzikbenh hey thanks, I created an Image Model but can you explain the ImageRequest is that something built into Laravel?

IsaacBen's avatar

@guezandy You create the request yourself. On the command line you enter "php artisan make:request ImageRequest". This is what I have in that file.

    public function authorize()
    {
        return true;
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return [
            'filename' => 'image|max:5000', -->That's my filter
            'no_email' => 'honey_pot'

        ];
    }
IsaacBen's avatar

@StuffedGoat I'm not just firing a message, the return stops the upload process so no matter what is uploaded it won't be saved.

1 like
guezandy's avatar
guezandy
OP
Best Answer
Level 1

I finally found that if I added the AmazonS3FullAccess policy under the user's permission under IAM the application finally worked and I was able to create and upload items to the bucket using:

    \Storage::disk('s3')->put('file.txt', 'Content');

1 like

Please or to participate in this conversation.