GuntarV's avatar
Level 40

Img validation error: "The given data was invalid." when uploading images greater than 2mb

I got Laravel/Vue project. In the project I created single/multiple image upload form. Images are being uploaded via ajax/Axios.

What I noticed is that when I try to upload an image that is greater than 2MB, I get and "The given data was invalid." error. With a lot of testing/trying, I noticed that the 2MB is the limit.

I did updated an php.ini to upload_max_filesize = 20M, but still the same error message. I am developing on Win 8.1 and using PHP V7.4

Any ideas/suggestions on what needs to be tweaked? My guess is that this is a PHP/Server issue, not the code I wrote in Laravel/Vue.

Please let me know if you have any ideas/suggestions? Thank you.

0 likes
8 replies
siangboon's avatar

the post_max_size??? try...

upload_max_filesize = 20M

post_max_size = 21M

GuntarV's avatar
Level 40

@siangboon , thank you for you suggestion. Hmmm, after changing the php.ini and restarting, I do see post_max_size = 21M and upload_max_filesize = 20M, but still images > 2MB are not uploading, I am getting the same error message.

siangboon's avatar

"The given data was invalid." error does not seem to be the error for upload file size limit... probably check the log and screenshot the log,error and code also may help other to understand more...

btw, did you include ""enctype="multipart/form-data"" in the form??

GuntarV's avatar
Level 40

@siangboon, when I upload images greater than 2MB, I still get the "The given data was invalid" validation error msg, and from what I see the validation msg is correct, because the uploaded file is not mimeType: "image/jpeg". There are no PHP errors in the log file. If I dd($request['files']); in the controller, for images greater than 2MB I see this:

Illuminate\Http\UploadedFile {#635
  -test: false
  -originalName: "IMG_1857.JPG"
  -mimeType: "application/octet-stream"
  -error: 1
  #hashName: null
  path: ""
  filename: ""
  basename: ""
  pathname: ""
  extension: ""
  realPath: "E:\wamp64\www\agnote_app\public"
  aTime: 1970-01-01 00:00:00
  mTime: 1970-01-01 00:00:00
  cTime: 1970-01-01 00:00:00
  inode: false
  size: false
  perms: 00
  owner: false
  group: false
  type: false
  writable: false
  readable: false
  executable: false
  file: false
  dir: false
  link: false
}

for images that are less than 2MB,

Illuminate\Http\UploadedFile {#635
  -test: false
  -originalName: "IMG_1860.JPG"
  -mimeType: "image/jpeg"
  -error: 0
  #hashName: null
  path: "E:\wamp64\tmp"
  filename: "phpDC33.tmp"
  basename: "phpDC33.tmp"
  pathname: "E:\wamp64\tmp\phpDC33.tmp"
  extension: "tmp"
  realPath: "E:\wamp64\tmp\phpDC33.tmp"
  aTime: 2020-05-28 21:16:20
  mTime: 2020-05-28 21:16:20
  cTime: 2020-05-28 21:16:20
  inode: 7318349394891073
  size: 1936411
  perms: 0100666
  owner: 0
  group: 0
  type: "file"
  writable: true
  readable: true
  executable: false
  file: true
  dir: false
  link: false
  linkTarget: "E:\wamp64\tmp\phpDC33.tmp"
GuntarV's avatar
Level 40

Also, below is what I currently have in my php.ini for Data Handling and File Uploads (a lot of the comments are removed):

;;;;;;;;;;;;;;;;;
; Data Handling ;
;;;;;;;;;;;;;;;;;


; http://php.net/variables-order
variables_order = "GPCS"


; http://php.net/request-order
request_order = "GP"

; http://php.net/register-argc-argv
register_argc_argv = Off


; http://php.net/auto-globals-jit
auto_globals_jit = Off

; http://php.net/post-max-size
post_max_size = 21M

; Automatically add files before PHP document.
; http://php.net/auto-prepend-file
auto_prepend_file =

; Automatically add files after PHP document.
; http://php.net/auto-append-file
auto_append_file =

; http://php.net/default-mimetype
default_mimetype = "text/html"

; http://php.net/default-charset
default_charset = "UTF-8"

;;;;;;;;;;;;;;;;
; File Uploads ;
;;;;;;;;;;;;;;;;

; Whether to allow HTTP file uploads.
; http://php.net/file-uploads
file_uploads = On

; Temporary directory for HTTP uploaded files (will use system default if not
; specified).
; http://php.net/upload-tmp-dir
upload_tmp_dir ="e:/wamp64/tmp"

; Maximum allowed size for uploaded files.
; http://php.net/upload-max-filesize
upload_max_filesize = 20M

; Maximum number of files that can be uploaded via a single request
max_file_uploads = 20
siangboon's avatar

how about temporarily increase the below setting just for testing?

max_execution_time=900
memory_limit=60M
max_input_time=900
GuntarV's avatar
Level 40

Hmm..., this is a "tricky one". I did made the changes, but still, < 2mb is OK, > 2mb. throws an error. I am also noticing, when the image is close to 2mb, it takes, sometimes up to few seconds, to upload, but when I select an image > 2mb, the 422 comes back instantly, it doesn't even try. In few days I'll be pushing stuff up to staging server, curious to see how it will behave there, possible that only my local development is not properly configured.

What is also interesting, that I can upload 6 images at once, as long as each image is less than 2mb, it works/uploads the files.

GuntarV's avatar
Level 40

Ok, little embarrassing, but finally figured it out. My issue was that I have multiple PHP versions installed, and normally I change the php.ini by boing through Wamp menu (menu does show the correct PHP version), but some how it points to different version PHP.ini. Anyhow, I found the code below, which I found on: https://stackoverflow.com/questions/26209377/how-can-i-upload-a-file-in-php-large-than-about-2mb Ran the code in editor and yes, the upload_max_filesize was set @ 2mb. Below is the code, if anyone else comes a cross similar issue, try running the code first.

echo ini_get("memory_limit")."\n";
ini_set("memory_limit","30M");
echo ini_get("memory_limit")."\n";

echo ini_get("post_max_size")."\n";
ini_set("post_max_size","20M");
echo ini_get("post_max_size")."\n";

echo ini_get("upload_max_filesize")."\n";
ini_set("upload_max_filesize","19M");
echo ini_get("upload_max_filesize")."\n";

Thank you @siangboon for all your help!

1 like

Please or to participate in this conversation.