0

i want to check the file is selected or not in controller if not selected then show error else upload on server. i create a html element like this <input type="file" name="car_gallery[]" multiple /> and my form has also this attribute enctype="multipart/form-data". in controller i check like this

if($this->request->getFileMultiple('car_gallery')){
}

but this condition is always true even i did not select any file. what is solution for multiple file upload its work fine on single image

2
  • did you try var_dump($this->request->getFileMultiple('car_gallery')); and see why it's marked true? Commented Dec 2, 2021 at 13:37
  • Please provide enough code so others can better understand or reproduce the problem. Commented Dec 7, 2021 at 16:38

2 Answers 2

1

I am not sure about this either as I am having this problem too. But, I am logging (via print_r) the call to $this->request->getFileMultiple('my_file_name_here') and noticed when I submitted my form without having a file attached, a file object was created (hence returning TRUE - I think). The object has all the properties empty, except the error property is set to 4 and if this maps to PHP's File Upload Errors, it means no file was uploaded.

I wonder if the object is created specifically because of this error?

Here is the code from my logging when I submit the form with no file attached.

(
    [0] => CodeIgniter\HTTP\Files\UploadedFile Object
        (
            [path:protected] => 
            [originalName:protected] => 
            [name:protected] => 
            [originalMimeType:protected] => 
            [error:protected] => 4
            [hasMoved:protected] => 
            [size:protected] => 0
            [pathName:SplFileInfo:private] => 
            [fileName:SplFileInfo:private] => 
        )

)

I honestly don't know how to (or not to) check for an if($this->request->getFileMultiple('my_file_name_here')) if it is going to always be true whether a file is attached or not.

I could be totally wrong in all this. I am up for any help and suggestions.

If this is totally wrong or not in accord with the site rules, take it down by all means.

Sign up to request clarification or add additional context in comments.

Comments

-1

Just use getFiles().

$this->request->getFiles('my_file_name_here')

1 Comment

Your answer could be improved with additional supporting information. Please edit to add further details, such as citations or documentation, so that others can confirm that your answer is correct. You can find more information on how to write good answers in the help center.

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.