• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Invalid argument for foreach

#1
Hi,

I'm new in this forum and so i apologize if my message isn't fully respectfull of your traditions... Moreover, i'm not very good in english... so i'm sorry if i make errors in writting.

I can't say if the bug i have in my application is fully linked to PHP 7.2 but i observe that i have problem when i configure my server in 7.2 and i have no problem when i configure my server in 7.1.

My CodeIgniter is 3.1.5 version and this is error i have with PHP 7.2 :

A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: controllers/Newsmanager.php

Line Number: 127

Backtrace:

File: /home/nbbeliar/public_html/ex/igniter/application/controllers/[b][i]Newsmanager.php[/i][/b]
Line: 127
Function: _error_handler

File: /home/nbbeliar/public_html/ex/igniter/index.php
Line: 315
Function: require_once

Here the PHP code :

PHP Code:
if ( ! $this->upload->do_upload('userfile'))
 
   {
 
     $infos $this->upload->display_errors();
 
     
      $datas
['infos'] = $infos;
 
     $datas['info'] = '';
 
     foreach $infos as $info // THIS IS THE LINE 127
 
     {
 
       $datas['info'] .= '<p>'.$info.'</p>';
 
     }
 
     
      own_viewLoaderConnected
('newsmanager/newsupload.php'$datas);
 
   

I think it's an indirect error. Because with PHP 7.1, my upload succeed, so this section isn't started with 7.1 (the upload succeed) and i've understood that


PHP Code:
display_errors(); 


function don't have an array result but a string result... what explains the foreach error. But that doesn't explain the reason why upload works perfectly with PHP 7.1 and doesn't work anyway with PHP 7.2...

My full original PHP code for this upload section...

PHP Code:
public function addnewssumbit()
 
 {
 
   // Verifying user is an admin, because only admin can add a document into the system
 
   $usergroups = array();
 
   if is_array($this->session->usergroups) )
 
   {
 
     foreach $this->session->usergroups as $group )
 
     array_push$usergroups$group['ag_shortname'] ); }
 
   }
 
   
    if 
( ( !in_array'admin_all'$usergroups ) ) OR ( !is_array($this->session->usergroups) ) )
 
    
      exit
('Accès interdit à cette fonctionnalité ou cet espace de la plateforme.<br/>
            Vous ne faites pas partie du ou des groupes appropriés pour cela.<br/>
            <a href="javascript:history.back()"><b>Retour en arrière</b></a> - <a href="'
.base_url().'"><b>Retour à l\'accueil</b></a>');
 
   }
 
   
    
// Config for upload : only ZIP, with no limit of size
 
   $config['upload_path'         '/my/upload/path/';
 
   $config['allowed_types'       'zip';
 
   $config['max_size'            0;
 
   
    $this
->load->library('upload'$config);

 
   // Lauching upload and see the result... (error) - WITH PHP 7.2 upload has error
 
   if ( ! $this->upload->do_upload('userfile'))
 
   {
 
     $infos $this->upload->display_errors();
 
     
      $datas
['infos'] = $infos;
 
     $datas['info'] = '';
 
     foreach $infos as $info )
 
     {
 
       $datas['info'] .= '<p>'.$info.'</p>';
 
     }
 
     
      own_viewLoaderConnected
('newsmanager/newsupload.php'$datas);
 
   }
 
   
    
// Lauching upload and see the result... (success) - WITH PHP 7.1 upload succeed
 
   else
    
{
 
     $infos $this->upload->data();
 
     
      $datas
['infos'] = $infos;
 
     $datas['info'] = '<p>Succès du téléchargement du fichier : <b>'.$infos['full_path'].'</b></p>';
 
     
      
// Once the file is uploaded, we have to unzip it into the same folder
 
     $zip = new ZipArchive;
 
     
      
// If unzip was a success, we have to notice and to delete zip because it's useless
 
     if $zip->open$infos['full_path'] ) === TRUE 
 
     {
 
       $zip->extractTo($infos['file_path']);
 
       $zip->close();
 
       
        $datas
['info'] .= '<p>Extraction réussie dans le dossier des news</p>';
 
       
        
// Deleting zip file (success)
 
       if unlink($infos['full_path']) )
 
       {
 
         $datas['info'] .= '<p>Suppression du ZIP réussie</p>';
 
        
        
// Deleting zip file (error)
 
       else
        
{
 
         $datas['info'] .= '<p>Suppression du ZIP échouée</p>';
 
       }
 
     
 
     
      
// Unzip can failed...
 
     else 
      
{
 
       $datas['info'] .= '<p>Extraction échouée dans le dossier des news</p>';
 
     }
 
     
      
// Loading the view to show result
 
     own_viewLoaderConnected('newsmanager/newsupload.php'$datas);
 
   }
 
   
  


When i solve my error with $info = $this->upload->display_errors(); (using it as string and no as array... without for each), i have no one error message but... no one content into display_errors(); function  despite the fact my upload failed !

So i think something is going wrong with upload with PHP 7.2... and CodeIgniter 3.1.5 ? But i don't know what. So, i've set back my server to PHP 7.1... If it can help the team...

CodeIngiter is so cool for the beginner i am so thank you for your cool work !
Reply

#2
(01-13-2018, 12:37 AM)nbbeliard Wrote: Hi,

I'm new in this forum and so i apologize if my message isn't fully respectfull of your traditions... Moreover, i'm not very good in english... so i'm sorry if i make errors in writting.

I can't say if the bug i have in my application is fully linked to PHP 7.2 but i observe that i have problem when i configure my server in 7.2 and i have no problem when i configure my server in 7.1.

My CodeIgniter is 3.1.5 version and this is error i have with PHP 7.2 :

A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: controllers/Newsmanager.php

Line Number: 127

Backtrace:

File: /home/nbbeliar/public_html/ex/igniter/application/controllers/[b][i]Newsmanager.php[/i][/b]
Line: 127
Function: _error_handler

File: /home/nbbeliar/public_html/ex/igniter/index.php
Line: 315
Function: require_once

Here the PHP code :

PHP Code:
if ( ! $this->upload->do_upload('userfile'))
 
   {
 
     $infos $this->upload->display_errors();
 
     
      $datas
['infos'] = $infos;
 
     $datas['info'] = '';
 
     foreach $infos as $info // THIS IS THE LINE 127
 
     {
 
       $datas['info'] .= '<p>'.$info.'</p>';
 
     }
 
     
      own_viewLoaderConnected
('newsmanager/newsupload.php'$datas);
 
   

I think it's an indirect error. Because with PHP 7.1, my upload succeed, so this section isn't started with 7.1 (the upload succeed) and i've understood that


PHP Code:
display_errors(); 


function don't have an array result but a string result... what explains the foreach error. But that doesn't explain the reason why upload works perfectly with PHP 7.1 and doesn't work anyway with PHP 7.2...

My full original PHP code for this upload section...

PHP Code:
public function addnewssumbit()
 
 {
 
   // Verifying user is an admin, because only admin can add a document into the system
 
   $usergroups = array();
 
   if is_array($this->session->usergroups) )
 
   {
 
     foreach $this->session->usergroups as $group )
 
     array_push$usergroups$group['ag_shortname'] ); }
 
   }
 
   
    if 
( ( !in_array'admin_all'$usergroups ) ) OR ( !is_array($this->session->usergroups) ) )
 
    
      exit
('Accès interdit à cette fonctionnalité ou cet espace de la plateforme.<br/>
            Vous ne faites pas partie du ou des groupes appropriés pour cela.<br/>
            <a href="javascript:history.back()"><b>Retour en arrière</b></a> - <a href="'
.base_url().'"><b>Retour à l\'accueil</b></a>');
 
   }
 
   
    
// Config for upload : only ZIP, with no limit of size
 
   $config['upload_path'         '/my/upload/path/';
 
   $config['allowed_types'       'zip';
 
   $config['max_size'            0;
 
   
    $this
->load->library('upload'$config);

 
   // Lauching upload and see the result... (error) - WITH PHP 7.2 upload has error
 
   if ( ! $this->upload->do_upload('userfile'))
 
   {
 
     $infos $this->upload->display_errors();
 
     
      $datas
['infos'] = $infos;
 
     $datas['info'] = '';
 
     foreach $infos as $info )
 
     {
 
       $datas['info'] .= '<p>'.$info.'</p>';
 
     }
 
     
      own_viewLoaderConnected
('newsmanager/newsupload.php'$datas);
 
   }
 
   
    
// Lauching upload and see the result... (success) - WITH PHP 7.1 upload succeed
 
   else
    
{
 
     $infos $this->upload->data();
 
     
      $datas
['infos'] = $infos;
 
     $datas['info'] = '<p>Succès du téléchargement du fichier : <b>'.$infos['full_path'].'</b></p>';
 
     
      
// Once the file is uploaded, we have to unzip it into the same folder
 
     $zip = new ZipArchive;
 
     
      
// If unzip was a success, we have to notice and to delete zip because it's useless
 
     if $zip->open$infos['full_path'] ) === TRUE 
 
     {
 
       $zip->extractTo($infos['file_path']);
 
       $zip->close();
 
       
        $datas
['info'] .= '<p>Extraction réussie dans le dossier des news</p>';
 
       
        
// Deleting zip file (success)
 
       if unlink($infos['full_path']) )
 
       {
 
         $datas['info'] .= '<p>Suppression du ZIP réussie</p>';
 
        
        
// Deleting zip file (error)
 
       else
        
{
 
         $datas['info'] .= '<p>Suppression du ZIP échouée</p>';
 
       }
 
     
 
     
      
// Unzip can failed...
 
     else 
      
{
 
       $datas['info'] .= '<p>Extraction échouée dans le dossier des news</p>';
 
     }
 
     
      
// Loading the view to show result
 
     own_viewLoaderConnected('newsmanager/newsupload.php'$datas);
 
   }
 
   
  


When i solve my error with $info = $this->upload->display_errors(); (using it as string and no as array... without for each), i have no one error message but... no one content into display_errors(); function  despite the fact my upload failed !

So i think something is going wrong with upload with PHP 7.2... and CodeIgniter 3.1.5 ? But i don't know what. So, i've set back my server to PHP 7.1... If it can help the team...

CodeIngiter is so cool for the beginner i am so thank you for your cool work !

Update to 3.1.6.

Is it on the same server or different? Check the upload folder permission
Reply

#3
Hi and TY for your answer.

It's the same server, my provider lets us choose PHP version we want. I have a "selector" in cPanel. So i'm absolutly sure it's the single difference between the 2 tests.

I understand why you move my post but i repeat my problem isn't the foreach warning... I have solved it alone. I never met this problem before because i've never got error with uploading before... So this condition never be used.

The main question is : why upload function failed with PHP 7.2 when it perfectly works with PHP 7.1 ? Permissions are same... and no one other parameter is different. I have no one PHP error... and i have no one $this->upload->display_errors(); return... hum...

So it's not a problem for me because i can stay with PHP 7.1... I just thought that it could be usefull for you to know this difficulty exists as exemple of PHP version changing complications.

Maybe I'll try to upload codeigniter to 3.1.6 but I have to study the update procedure first. If I do, I will give you news about new PHP 7.2 try.
Reply

#4
(01-13-2018, 01:24 AM)nbbeliard Wrote: Hi and TY for your answer.

It's the same server, my provider lets us choose PHP version we want. I have a "selector" in cPanel. So i'm absolutly sure it's the single difference between the 2 tests.

I understand why you move my post but i repeat my problem isn't the foreach warning... I have solved it alone. I never met this problem before because i've never got error with uploading before... So this condition never be used.

The main question is : why upload function failed with PHP 7.2 when it perfectly works with PHP 7.1 ? Permissions are same... and no one other parameter is different. I have no one PHP error... and i have no one $this->upload->display_errors(); return... hum...

So it's not a problem for me because i can stay with PHP 7.1... I just thought that it could be usefull for you to know this difficulty exists as exemple of PHP version changing complications.

Maybe I'll try to upload codeigniter to 3.1.6 but I have to study the update procedure first. If I do, I will give you news about new PHP 7.2 try.

upload work with php 7.2. Check the php error log
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


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