1

I'm handling a file field with ajax request (FormData) to send an image to server.

Here is my php ajax handler

public function onUploadAvatar()
{
$photo = Request::file('avatar');

$avatar = new Controllers\AvatarController( $photo );

$validator = $avatar->validator();

if ( $validator->fails() )
{
  return [
    'success' => false,
    'validationErrors' => $validator->errors()
  ];
}

$moved = $avatar->attach();

if ( $moved !== false )
{
  return [
    'success' => true,
    'avatarPath' => $moved
  ];
}
}

Below my validator method

  public function validator()
  {
    $input = [
      'avatar' => $this->photo
    ];

    $rules = [
      'avatar' => 'image|mimes:jpeg,jpg,png|max:1048576'
    ];

    $messages = [
      'mimes' => 'invalid format',
      'max' => 'please 1MB',
      'image' => 'please image'
    ];

    $validator = Validator::make( $input, $rules, $messages );

    return $validator;
  }

When i send some file with size less than max rule, i have the expected response:

enter image description here enter image description here

THE PROBLEM

But when i upload file with more than max, i have this (response) error:

enter image description here enter image description here

The anothers validation rules is working.

ADITIONAL INFORMATION

Returning some data after request:

return [
  'photo' => Request::file('avatar'),
  'hasFile' => Request::hasFile('avatar'),
  'extension' => Request::file('avatar')->getClientOriginalExtension(),
  'size' => Request::file('avatar')->getClientSize()
];

RESPONSES:

File less than max:

{"photo":{},"hasFile":true,"extension":"jpg","size":900498}

File with more than max:

{"photo":{},"hasFile":false,"extension":"jpg","size":0}

SOLUTION

I expected some feedback as The file "file.jpg" exceeds your upload_max_filesize ini directive for php.ini fails related to the max size. Only increased upload_max_filesize directive and worked. No more problem.

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.