CodeIgniter Forums
Upload PDF file (not required) - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Libraries & Helpers (https://forum.codeigniter.com/forumdisplay.php?fid=11)
+--- Thread: Upload PDF file (not required) (/showthread.php?tid=78036)



Upload PDF file (not required) - xibnoe - 11-22-2020

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?


RE: Upload PDF file (not required) - sammyskills - 11-23-2020

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




RE: Upload PDF file (not required) - InsiteFX - 11-25-2020

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

CodeIgniter 4 User Guide - Working with Uploaded Files