• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
File Upload Validation

#1
It appears that file upload validation is not working properly. I have the following:
PHP Code:
// $Id: Notice.php,v 1.3 2019/08/27 19:36:24 dwinston Exp $

use CodeIgniter\Controller;

class 
Notice extends Controller
{
    private 
$validation;
    
 
   public function __construct(...$params)
 
   {
 
          $this->validation  \Config\Services::validation();
 
      helper(['bnc''form']);
 
   }
 
   
    public 
function index()
 
   {
 
          print view('upload-notice', ['validation' => $this->validation]);
 
   }
 
   
    public 
function upload()
 
   {
 
          $messages = [
     
      'in_re' => [
                
'required' => 'In Re: is required',
         
  ],
 
              'notice' => [
 
                  'mime_in' => 'Notice must be a PDF file',
 
                  'ext_in' => 'Notice must have a pdf extension',
 
                  'max_size' => 'Notice is too large',
 
              ],
        ];
        
$rules = [
            
'in_re' => 'required',
         
   'notice' => 'mime_in[notice, application/pdf]|ext_in[notice, pdf]|max_size[notice, 2048]',
        ];
        if (
$this->request->getPost('submitted') == null || ! $this->validate($rules$messages)) 
         
   print view('upload-notice', ['validation' => $this->validation]);
        else {
         
   $env bnc_get_environment(); //bnc_helper
         
   $path "/BSG/data/bnc/$env/public-notices";
         
   if (! file_exists($path)) mkdir($path0745true);
         
   $filename time() . '.' $_FILES['notice']['name'];
         
   copy($_FILES['notice']['tmp_name'], "$path/$filename");
         
   print view('upload-msg', [
         
       'in_re' => $this->request->getPost('in_re'),
         
       'encoded_filename' => urlencode($filename),
         
   ]);
        }
 
   

Any pdf fails on mime type or extension validators. max size appears to work.
Simpler is always better
Reply

#2
oof!

'notice' => 'mime_in[notice, application/pdf]|ext_in[notice, pdf]|max_size[notice, 2048]',

should be

'notice' => 'mime_in[notice,application/pdf]|ext_in[notice,pdf]|max_size[notice,2048]',

I had a space after the comma.
Simpler is always better
Reply

#3
it's weird, this shouldn't be a error  Undecided
Reply

#4
(08-28-2019, 06:16 AM)website Wrote: it's weird, this shouldn't be a error  Undecided

Maybe it could be improved, but it's the way it work.
See: https://codeigniter4.github.io/CodeIgnit...able-rules

Rule is a string; there must be no spaces between the parameters, especially the “is_unique” rule. There can be no spaces before and after “ignore_value”.

“is_unique[supplier.name,uuid, $uuid]” is not ok
“is_unique[supplier.name,uuid,$uuid ]” is not ok
“is_unique[supplier.name,uuid,$uuid]” is ok
Test your translation files with Translation Tester
Reply

#5
(08-28-2019, 08:13 AM)includebeer Wrote:
(08-28-2019, 06:16 AM)website Wrote: it's weird, this shouldn't be a error  Undecided

Maybe it could be improved, but it's the way it work.

you're right, but it's surprising anyway Smile
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.