Welcome Guest, Not a member yet? Register   Sign In
file handling doesn't work properly CI4 PHP 7.4
#1

every time i use $this->request->getFile() or whenever I use built in image validation, it throws me error like
array_key_exists() is deprecated, please use isset() or property_exists().
I believe that the problem is at system\HTTP\files\FileCollection.php
And i am afraid to modify core codeigniter4 files. Please fix this. I have no problems before php v7.4
Reply
#2

And what version of CodeIgniter 4 are you running?
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#3

(06-30-2021, 09:01 PM)InsiteFX Wrote: And what version of CodeIgniter 4 are you running?

Hello, Sorry but $request->getFile() actually works but built in image validation rules does not because of deprecated array_key_exists(). I am using CI4 v 4.1.3
Reply
#4

Can you share the stack trace of the error? If you are getting that error, it means either you or the framework is using array_key_exists on an object, which is deprecated in 7.4
Reply
#5

Here's the stack trace for the problem mentioned. I'm having it too, exactly as the OP described. Using PHP 7.4.9 and CI 4.1.4

  1. {PHP internal code}   —  CodeIgniter\Debug\Exceptions->errorHandler ( arguments )

  2. SYSTEMPATH\HTTP\Files\FileCollection.php : 120   —   array_key_exists()
  3. SYSTEMPATH\HTTP\Files\FileCollection.php : 84   —  CodeIgniter\HTTP\Files\FileCollection->hasFile ( arguments )


    Code:
    77      *
    78      * @return array|null
    79      */
    80     public function getFileMultiple(string $name)
    81     {
    82         $this->populateFiles();
    83
    84         if ($this->hasFile($name)) {
    85             if (strpos($name, '.') !== false) {
    86                 $name         = explode('.', $name);
    87                 $uploadedFile = $this->getValueDotNotationSyntax($name, $this->files);
    88
    89                 return (is_array($uploadedFile) && ($uploadedFile[array_key_first($uploadedFile)] instanceof UploadedFile)) ?
    90                     $uploadedFile : null;
    91             }

  4. SYSTEMPATH\HTTP\IncomingRequest.php : 723   —  CodeIgniter\HTTP\Files\FileCollection->getFileMultiple ( arguments )


    Code:
    716      */
    717     public function getFileMultiple(string $fileID)
    718     {
    719         if ($this->files === null) {
    720             $this->files = new FileCollection();
    721         }
    722
    723         return $this->files->getFileMultiple($fileID);
    724     }
    725
    726     /**
    727      * Retrieves a single file by the name of the input field used
    728      * to upload it.
    729      *
    730      * @return UploadedFile|null

  5. SYSTEMPATH\Validation\FileRules.php : 85   —  CodeIgniter\HTTP\IncomingRequest->getFileMultiple ( arguments )


    Code:
    78     public function max_size(?string $blank, string $params): bool
    79     {
    80         // Grab the file name off the top of the $params
    81         // after we split it.
    82         $params = explode(',', $params);
    83         $name   = array_shift($params);
    84
    85         if (! ($files = $this->request->getFileMultiple($name))) {
    86             $files = [$this->request->getFile($name)];
    87         }
    88
    89         foreach ($files as $file) {
    90             if ($file === null) {
    91                 return false;
    92             }

  6. SYSTEMPATH\Validation\Validation.php : 277   —  CodeIgniter\Validation\FileRules->max_size ( arguments )


    Code:
    270                 // Check in our rulesets
    271                 foreach ($this->ruleSetInstances as $set) {
    272                     if (! method_exists($set, $rule)) {
    273                         continue;
    274                     }
    275
    276                     $found  = true;
    277                     $passed = $param === false ? $set->{$rule}($value, $error) : $set->{$rule}($value, $param, $data, $error);
    278
    279                     break;
    280                 }
    281
    282                 // If the rule wasn't found anywhere, we
    283                 // should throw an exception so the developer can find it.
    284                 if (! $found) {

  7. SYSTEMPATH\Validation\Validation.php : 151   —  CodeIgniter\Validation\Validation->processRules ( arguments )


    Code:
    144             if ($values === []) {
    145                 // We'll process the values right away if an empty array
    146                 $this->processRules($field, $setup['label'] ?? $field, $values, $rules, $data);
    147             }
    148
    149             foreach ($values as $value) {
    150                 // Otherwise, we'll let the loop do the job
    151                 $this->processRules($field, $setup['label'] ?? $field, $value, $rules, $data);
    152             }
    153         }
    154
    155         return $this->getErrors() === [];
    156     }
    157
    158     /**

  8. SYSTEMPATH\Controller.php : 160   —  CodeIgniter\Validation\Validation->run ()

    Code:
    153                 $errorName = $rules . '_errors';
    154                 $messages  = $validation->{$errorName} ?? [];
    155             }
    156
    157             $rules = $validation->{$rules};
    158         }
    159
    160         return $this->validator->withRequest($this->request)->setRules($rules, $messages)->run();
    161     }
    162 }
    163

  9. APPPATH\Controllers\Front\Imgs.php : 45   —  CodeIgniter\Controller->validate ( arguments )


    Code:
    38     }
    39
    40     public function create()
    41     {
    42         $model = new ImgsModel();
    43
    44         if ( $this->request->getMethod() === 'post' && $this->validate([
    45                 'title' => 'required|min_length[3]|max_length[255]',
    46                 'tags' => 'required|min_length[3]|max_length[255]',
    47                 'img' => 'uploaded[img]|max_size[img.2048]|mime_in[img.image/png.image/jpg.image/gif.video/mp4]',
    48         ]) ) {
    49             /*if (! $file->isValid()) {
    50                     throw new \RuntimeException($file->getErrorString().'('.$file->getError().')');
    51             }*/
    52

  10. SYSTEMPATH\CodeIgniter.php : 802   —  App\Controllers\Front\Pins->create ()

    Code:
    795     {
    796         // If this is a console request then use the input segments as parameters
    797         $params = defined('SPARKED') ? $this->request->getSegments() : $this->router->params(); // @phpstan-ignore-line
    798
    799         if (method_exists($class, '_remap')) {
    800             $output = $class->_remap($this->method, ...$params);
    801         } else {
    802             $output = $class->{$this->method}(...$params);
    803         }
    804
    805         $this->benchmark->stop('controller');
    806
    807         return $output;
    808     }
    809

  11. SYSTEMPATH\CodeIgniter.php : 399   —  CodeIgniter\CodeIgniter->runController ( arguments )


    Code:
    392             if (! method_exists($controller, '_remap') && ! is_callable([$controller, $this->method], false)) {
    393                 throw PageNotFoundException::forMethodNotFound($this->method);
    394             }
    395
    396             // Is there a "post_controller_constructor" event?
    397             Events::trigger('post_controller_constructor');
    398
    399             $returned = $this->runController($controller);
    400         } else {
    401             $this->benchmark->stop('controller_constructor');
    402             $this->benchmark->stop('controller');
    403         }
    404
    405         // If $returned is a string, then the controller output something,
    406         // probably a view, instead of echoing it directly. Send it along

  12. SYSTEMPATH\CodeIgniter.php : 317   —  CodeIgniter\CodeIgniter->handleRequest ( arguments )


    Code:
    310             $this->response->pretend($this->useSafeOutput)->send();
    311             $this->callExit(EXIT_SUCCESS);
    312
    313             return;
    314         }
    315
    316         try {
    317             return $this->handleRequest($routes, $cacheConfig, $returnResponse);
    318         } catch (RedirectException $e) {
    319             $logger = Services::logger();
    320             $logger->info('REDIRECTED ROUTE at ' . $e->getMessage());
    321
    322             // If the route is a 'redirect' route, it throws
    323             // the exception with the $to as the message
    324             $this->response->redirect(base_url($e->getMessage()), 'auto', $e->getCode());

  13. FCPATH\index.php : 37   —  CodeIgniter\CodeIgniter->run ()

    Code:
    30 /*
    31  *---------------------------------------------------------------
    32  * LAUNCH THE APPLICATION
    33  *---------------------------------------------------------------
    34  * Now that everything is setup, it's time to actually fire
    35  * up the engines and make this app do its thang.
    36  */
    37 $app->run();
    38

Reply
#6

@paulbalandan, I have varified this, the following line numbers in Rules.php are using array_key_exists.

Line Numbers:
34
196
290
and
328

CodeIgniter development version downloaded today at 3:00 am EST.
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply
#7

I should also note that I was using the file validation rules and uploading a file when this issue occurred. I used rules `uploaded[img]`, `max_size[img.2048]`, and `mine_in[img.image/png.image/jpg.image/gif.video/mp4]`.  Avoided the error by only using the `uploaded` rule.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB