Welcome Guest, Not a member yet? Register   Sign In
Upload PDF file (not required)
#1

I have problem with form validation for upload file, here my validation rule

'file' => [
              // 'uploaded[file]',
                'mime_in[file,application/pdf,image/jpg,image/jpeg,image/gif,image/png]',
                'max_size[file,4096]',
    ],


However, if I don't upload the image, it shows validation error. How to set this validation rule only to apply to an uploaded file?
Reply
#2

First, you need to check if the file was submitted before you do any validation. 

You can do so by using the [code]getFiles()[\code] function provided by CI. See documentation: Working with Uploaded Files — CodeIgniter 4.0.4 documentation

So your code would look like this:
PHP Code:
$uploaded_file $this->request->getFiles('file');

if ( !empty(
$uploaded_file) ) 
{
    // File was uploaded, run other validation here
}
else 
{
    // No file was uploaded

Reply
#3

You can read about it here in the CodeIgniter 4 - Users Guide.

CodeIgniter 4 User Guide - Working with Uploaded Files
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply




Theme © iAndrew 2016 - Forum software by © MyBB