PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, July 31, 2022

[FIXED] How to fix 'The file failed to upload.' error using any validation for image upload - Laravel 5.7

 July 31, 2022     file-upload, laravel, laravel-5, validation     No comments   

Issue

I am using Laravel 5.7

I am attempting to upload an image file and validating its upload using built in laravel validation. It keeps responding with 'The file failed to upload.'

I am using AJAX to upload this data. My form has enctype="multipart/form-data" set.

I have already figured out to include the CSRF token with the AJAX request.

I have already fixed the php.ini max_file_size issue, and the files I am testing with are far under 10MB.

If I upload a text file (as an example), and with validation set to required|image|max:10000 it will correctly prevent the file from uploading and respond with 'File must be an image.'

If I disable all validation, the file is uploaded just fine.

I cannot think of anything else I may be doing wrong. Please help as this as my project is at a halt at the moment.

Form HTML:

<form method="post" class="dropzone" action="{{ route('images') }}" enctype="multipart/form-data"
                  data-nopants-load="dropzone{'transformImage':true,'acceptedFiles-1':'image/jpg','previewsContainer':'#dropzone-previews'}">
                @method('post')
                @csrf
                <div class="dz-border">
                    <div class="background"></div>
                    <div class="border"></div>
                </div>
                <div class="dz-message margins-auto _font-size-large"><span>Drop Images here or <span class="_underline">Click</span> to Upload.</span></div>
                <div class="fallback flex-col">
                    <div class="self-center margin-bottom">
                        <input name="file" type="file" class="form-input" multiple />
                    </div>
                    <button type="submit" class="button -call">Upload</button>
                </div>
            </form>

UploadImageFormRequest.php

namespace App\Http\Requests;

use Illuminate\Foundation\Http\FormRequest;

use Illuminate\Support\Facades\Auth;

class UploadImageFormRequest extends FormRequest
{
    /**
     * Determine if the user is authorized to make this request.
     *
     * @return bool
     */
    public function authorize()
    {
        return Auth::check();//user()->can('create', Organization::class);
    }

    /**
     * Get the validation rules that apply to the request.
     *
     * @return array
     */
    public function rules()
    {
        return ['file' => 'required|image|mimes:jpeg,bmp,png'];
    }
}

UploadImagesController.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests\UploadImageFormRequest;

class UploadImagesController extends Controller
{
    public function __construct()
    {
        $this->middleware('auth:user,admin');
    }

    public function store(UploadImageFormRequest $request){
    //Wont get here...
    }
}

Solution

i think the multiple attribute turn it into array try remove the multiple attribute

<input name="file" type="file" class="form-input" multiple />

to this

<input name="file" type="file" class="form-input"/>

and if you want to have multiple images you should do this

<input name="file[]" type="file" class="form-input" multiple />

and your validation like this

        return [
      'file' => 'array',
      'file.*' => 'image|mimes:jpeg,bmp,png'
];


Answered By - Ahmed Aboud
Answer Checked By - Clifford M. (PHPFixing Volunteer)
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing