Skip to content
Advertisement

Laravel 8 file upload validation fails with any rule

I want to validate a file upload but with literally any validation rule, I get “The (name of input) failed to upload.” I’ve seen this issue in a few places but none of the solutions worked for me.

I’m using Laravel 8.0, php 8.0.2, and nginx/1.18.0 on ubuntu.

Controller:

JavaScript

Blade file:

JavaScript

If I get rid of the validation rule for ‘file’ it works, I get to the dd.

JavaScript

I’ve seen other people have this issue and I’ve tried:

  • Putting another rule (max:10000, image) instead of ‘required’ for the file – still get the same error.
  • Changing the values php.ini to post_max_size = 201M and upload_max_filesize = 200M (this shouldn’t be an issue in the first place because the image I am trying to upload is a 136kb jpg). Verified with phpinfo();
  • After changing these values, reloading nginx and php-fpm
  • Rebooting the VM
  • Checking that all the double and single quotes are the correct character
  • Trying to upload other filetypes like png or txt, same error.
  • Putting ‘image’ as the first validation rule which worked for someone here
  • Removing the extra comma at the end of the rules array
  • Changing the name of the file input to something other than ‘file’
  • Using a different browser (Firefox and Chrome)
  • Disabling all my browser extensions
  • Writing the validation like this instead (still get the same error):
JavaScript

If I dd($request) before validating:

JavaScript

Advertisement

Answer

Error value of 6 means UPLOAD_ERR_NO_TMP_DIR. Ensure that your system has a properly configured upload temp directory by running php -i from command line (or phpinfo(); from a web page) and checking for the upload_tmp_dir key. On a typical Linux system this will be something like /tmp. You can set the value in php.ini if needed. Ensure permissions are correct on the listed folder, such that the web server process is allowed to write to it.

Do not attempt to use one of your publicly-accessible folders as an upload directory (e.g. saving directly to storage/app/public or similar.) Your application code should move the file into storage as described in the documentation; something like this:

JavaScript
User contributions licensed under: CC BY-SA
1 People found this is helpful
Advertisement