Welcome Guest, Not a member yet? Register   Sign In
Invalid argument supplied for foreach()
#1

[eluser]SaSa[/eluser]
I have this error, How do I solve it?

Code:
A PHP Error was encountered
    Severity: Warning
    Message: Invalid argument supplied for foreach()
    Filename: admin/tour.php
    Line Number: 81

this is line 81:
Code:
$files = $this->multi_upload->go_upload();
            var_dump($files);
            $images = array();    
            foreach ($files as $img) {   //line 81
              $images[] = $img['file'];
            }
if (!$files)
        {
            //$error = array('error' => $this->upload->display_errors());
            //$this->load->view('admin/tour_foreign_insert', $error);
            //$data['error'] = $this->upload->display_errors();
            $this->session->set_flashdata('error', $this->upload->display_errors());
            //$this->load->view('admin/tour_foreign_insert',$data);
            //sleep(5);
            redirect('admin/tour/insert_foreign', 'refresh');
        }

With respect
#2

[eluser]DirkZz[/eluser]
What does $files contain?
#3

[eluser]SaSa[/eluser]
multi upload for file, Please help me...
#4

[eluser]SaSa[/eluser]
this is function go_uploadSadPlease help me...)
Code:
function go_upload($field = 'userfile') {
        $CI =& get_instance();
        // Is $_FILES[$field] set? If not, no reason to continue.
        if ( ! isset($_FILES[$field]['name'][0]))
        {
            $CI->upload->set_error('upload_no_file_selected');
            return FALSE;
        } else
        {
            $num_files = count($_FILES[$field]['name']) -1;
            $file_list = array();
            $error_hold = array();
            $error_upload = FALSE;
        }
        if ( ! $CI->upload->validate_upload_path())
        {
            return FALSE;
        }
        for ($i=0; $i < $num_files; $i++) {
            $error_hold[$i] = FALSE;
            if ( ! is_uploaded_file($_FILES[$field]['tmp_name'][$i]))
            {
                $error = ( ! isset($_FILES[$field]['error'][$i])) ? 4 : $_FILES[$field]['error'][$i];
                switch($error)
                {
                    case 1:  // UPLOAD_ERR_INI_SIZE
                        $error_hold[$i] = 'upload_file_exceeds_limit';
                        break;
                    case 2: // UPLOAD_ERR_FORM_SIZE
                        $error_hold[$i] = 'upload_file_exceeds_form_limit';
                        break;
                    case 3: // UPLOAD_ERR_PARTIAL
                       $error_hold[$i] = 'upload_file_partial';
                        break;
                    case 4: // UPLOAD_ERR_NO_FILE
                       $error_hold[$i] = 'upload_no_file_selected';
                        break;
                    case 6: // UPLOAD_ERR_NO_TMP_DIR
                        $error_hold[$i] = 'upload_no_temp_directory';
                        break;
                    case 7: // UPLOAD_ERR_CANT_WRITE
                        $error_hold[$i] = 'upload_unable_to_write_file';
                        break;
                    case 8: // UPLOAD_ERR_EXTENSION
                        $error_hold[$i] = 'upload_stopped_by_extension';
                        break;
                    default :
                        $error_hold[$i] = 'upload_no_file_selected';
                        break;
                }
                return FALSE;
            }
            $CI->upload->file_temp = $_FILES[$field]['tmp_name'][$i];
        $CI->upload->file_name = $_FILES[$field]['name'][$i];
            $CI->upload->file_size = $_FILES[$field]['size'][$i];        
            $CI->upload->file_type = preg_replace("/^(.+?);.*$/", "\\1", $_FILES[$field]['type'][$i]);
            $CI->upload->file_type = strtolower($CI->upload->file_type);
            $CI->upload->file_ext  = $CI->upload->get_extension($_FILES[$field]['name'][$i]);            
            if ($CI->upload->file_size > 0)
            {
                $CI->upload->file_size = round($CI->upload->file_size/1024, 2);
            }
            if ( ! $CI->upload->is_allowed_filetype())
            {
                $error_hold[$i] = 'upload_invalid_filetype';
            }
            if ( ! $CI->upload->is_allowed_filesize())
            {
                $error_hold[$i] = 'upload_invalid_filesize';
            }
            if ( ! $CI->upload->is_allowed_dimensions())
            {
                $error_hold[$i] = 'upload_invalid_dimensions';
            }
            $CI->upload->file_name = $CI->upload->clean_file_name($CI->upload->file_name);
            if ($CI->upload->remove_spaces == TRUE)
            {
                $CI->upload->file_name = preg_replace("/\s+/", "_", $CI->upload->file_name);
            }
            $CI->upload->orig_name = $CI->upload->file_name;

            if ($CI->upload->overwrite == FALSE)
            {
                $CI->upload->file_name = $CI->upload->set_filename($CI->upload->upload_path, $CI->upload->file_name);
                
                if ($CI->upload->file_name === FALSE)
                {
                    $error_hold[$i] = TRUE;
                }
            }
            if ( ! @copy($CI->upload->file_temp, $CI->upload->upload_path.$CI->upload->file_name))
            {
                if ( ! @move_uploaded_file($CI->upload->file_temp, $CI->upload->upload_path.$CI->upload->file_name))
                {
                     $error_hold[$i] = 'upload_destination_error';
                }
            }
            if ($CI->upload->xss_clean == TRUE)
            {
                $CI->upload->do_xss_clean();
            }
            
            if ($error_hold[$i]) {
                $error_upload = TRUE;
            } else {
                if ($imageVar = $this->multiple_image_properties($CI->upload->upload_path.$CI->upload->file_name)) {

                    $file_list[] = array(
                            'name' => $CI->upload->file_name,
                            'file' => $CI->upload->upload_path.$CI->upload->file_name,
                            'size' => $CI->upload->file_size,
                            'ext' => $CI->upload->file_ext,
                            'image_type' => $imageVar->image_type,
                            'height' => $imageVar->height,
                            'width' => $imageVar->width
                            );
                } else {
                    $file_list[] = array(
                            'name' => $CI->upload->file_name,
                            'file' => $CI->upload->upload_path.$CI->upload->file_name,
                            'size' => $CI->upload->file_size,
                            'type' => $CI->upload->file_type,
                            'ext' => $CI->upload->file_ext,
                            );
                }
            }      
        if ($error_upload) {
            $this->set_error($error_hold);
            return FALSE;
        } else {
            return $file_list;
        }    
    }
#5

[eluser]SaSa[/eluser]
Please help me…
#6

[eluser]NeoArc[/eluser]
Code:
if(is_array($files)){
            $images = array();    
            foreach ($files as $img) {   //line 81
              $images[] = $img['file'];
            }
} else {
  echo 'Something went wrong with the upload';
}
#7

[eluser]SaSa[/eluser]
thanks, but there is still problem, if use of $files = (array) $this->multi_upload->go_upload(); or if ($files !== FALSE) or if(is_array($files)) always is this messag(return false): The upload path does not appear to be valid. if use of if (count($files) > 0) same error show me:
Code:
A PHP Error was encountered

Severity: Warning

Message: Invalid argument supplied for foreach()

Filename: admin/tour.php

Line Number: 81

my CI_Controller:
Code:
function insert_data(){
        $jdate = jgmdate("j F Y");
        /*////////////////////////////////////*/
        $useradmin = $this->session->userdata('login');
        $query = $this->db->get_where('login', array('useradmin' => $useradmin))->row();
        $nameadmin = $query->nameadmin;
        $lastnameadmin = $query->lastnameadmin;
        //////////////////////////////////////////
        $config['upload_path'] = './uploads/'; // server directory
        $config['allowed_types'] = 'gif|jpg|png'; // by extension, will check for whether it is an image
        $config['max_size']    = '1000'; // in kb
        $config['max_width']  = '1024';
        $config['max_height']  = '768';
        
        $this->load->library('upload', $config);
        $this->load->library('Multi_upload');
                
        $files = $this->multi_upload->go_upload();
        if (count($files) > 0){
            $images = array();
            foreach ($files as $img) {
              $images[] = $img['file'];
            }
        }else{
            
            //$error = array('error' => $this->upload->display_errors());
            //$this->load->view('admin/tour_foreign_insert', $error);
            //$data['error'] = $this->upload->display_errors();
            $this->session->set_flashdata('error', $this->upload->display_errors());
            //$this->load->view('admin/tour_foreign_insert',$data);
            //sleep(5);
            redirect('admin/tour/insert_foreign', 'refresh');
        }                    
        ///////////////////////////////////////////
        $data = array (
                'name' => $this->input->post('name'),
        'term' => $this->input->post('term'),
        'hotel' => serialize($this->input->post('hotel')),
        'guide' => serialize($this->input->post('guide')),
        'airline' => serialize($this->input->post('airline')),
        'time_go' => $this->input->post('time_go'),
        'time_back' => $this->input->post('time_back'),
        'date_go' => $this->input->post('date_go'),
        'date_back' => $this->input->post('date_back') ,
                'service' => serialize($this->input->post('service')),
        'description' => $this->input->post('description'),
                'surf' => serialize($this->input->post('surf')),
                'type' => $this->input->post('type'),
        'terminal' => $this->input->post('terminal'),
        'degree' => $this->input->post('degree'),
        'first_terminal' => $this->input->post('first_terminal'),
        'end_terminal' => $this->input->post('end_terminal'),
        'image' => serialize($images),
                'date_submit' => $jdate,
        'useradmin_submit' => $nameadmin.'&nbsp;'.$lastnameadmin ,
    );
    $this->db->insert('tour_foreign', $data);
    }
what do i do?
#8

[eluser]NeoArc[/eluser]
The upload path does not appear to be valid

That is the real problem. Make sure the /uploads/ directory exists, in the same directory of your index.php file.


PD: And use is_array() if you are passing data from unknown sources to foreach.
#9

[eluser]SaSa[/eluser]
[quote author="NeoArc" date="1311870985"]The upload path does not appear to be valid

That is the real problem. Make sure the /uploads/ directory exists, in the same directory of your index.php file.


PD: And use is_array() if you are passing data from unknown sources to foreach.[/quote]

thanks, i tried again.i is sure the /uploads/ directory exists, if use of this is_array(), always have this message: The upload path does not appear to be valid.
What can I do?
#10

[eluser]NeoArc[/eluser]
Try this (var_export() it first ):

$config['upload_path'] = FCPATH.'uploads'.DIRECTORY_SEPARATOR;




Theme © iAndrew 2016 - Forum software by © MyBB