how can convert $files contain to array?

后端 未结 6 1375
[愿得一人]
[愿得一人] 2020-12-11 14:01

How do I solve this error?

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


        
相关标签:
6条回答
  • 2020-12-11 14:23

    I figure I would just share how I handle multiple uploads in CI, it's a little different than your method:

    Form looks like this:

    <?php echo form_open_multipart('upload/multiple_upload');?>
    <input type="file" name="userfile[]" size="20" /><br />
    <input type="file" name="userfile[]" size="20" /><br />
    </form>
    

    Upload/multiple_upload function in controller looks like this:

    function multiple_upload()
    {
        $config['upload_path'] = './userpics/originals/'; // server directory
        $config['allowed_types'] = 'gif|jpg|png'; // by extension, will check for whether it is an image
        $config['max_size']    = '512'; // in kb
        //$config['max_width']  = '1024';
        //$config['max_height']  = '768';
    
        $this->load->library('myupload');
        $error = array(); $data = array();
        for ($i=0;$i<count($_FILES['userfile']['name']);$i++) {
                $this->myupload->initialize($config);
                if (!$this->myupload->do_upload('userfile',$i)) {
                        $error[] = $this->myupload->display_errors();
                }
                $data[$i] = $this->myupload->data(); //gradually build up upload->data()
        }
    
        if ( count($error) > 0 )        
        {
            print_r($error);
        }    
        else
        {
            print_r($data);
        }
    }    
    

    At this point I made a copy of CI upload class, and pasted it into my applications library directory. A few changes need to be made. I named it myupload.

    @ Line 143:

    public function do_upload($field = 'userfile')
    

    change to

    public function do_upload($field = 'userfile', $i = 0)
    

    Any lines in this function above line 200, you must add [$i] to the end of the $_FILES variables.

    Example:

    is_uploaded_file($_FILES[$field]['tmp_name'])
    

    change to:

    is_uploaded_file($_FILES[$field]['tmp_name'][$i])
    

    There are 9 lines to be updated.

    0 讨论(0)
  • 2020-12-11 14:25

    Actually doing like this you wil get the file or the value you want to.

    foreach ($files as $img) {   //line 81
      echo $img; // echo the Var and you will see..
    }
    

    This line -> $files = $this->multi_upload->go_upload(); probably come from a file object in your form, right? Then it must to be named like this file[] and not only file. You need to be specific that it is an Array. Otherwise nothing will happen.

    0 讨论(0)
  • 2020-12-11 14:36

    Are you sure that you get an array? is see several return FALSE. So you should check,

    if ($files !== FALSE)
    

    or casting it to an (empty) array (if false returns) with

    $files = (array) $this->multi_upload->go_upload();
    
    0 讨论(0)
  • 2020-12-11 14:42

    The simple answer is that $files is not an array. foreach only works on arrays. We don't know what the value is, but in any case, it's not right. You'd have to look at the go_upload function to see why it's not doing what you expect.

    0 讨论(0)
  • 2020-12-11 14:47

    Too avoid seeing this warning message you should count the array:

    if (count($files) > 0)
    {
        foreach ($files as $img) {   //line 81
            $images[] = $img['file'];
        }
    }
    
    0 讨论(0)
  • 2020-12-11 14:48

    Regarding the 'Invalid argument supplied for foreach()' error this is probably because $files is not an array (or similar). The best way to do this IMHO is:

        $files = $this->multi_upload->go_upload();
    
        echo '<pre>';
        echo "Files:\n";
        print_r($files);
        echo '</pre>';
    
        $images = array();
        foreach ($files as $img) {  //this is line 80
          $images[] = $img['file'];
        }
        if ( ! $files )
        {
            $this->session->set_flashdata('error', $this->upload->display_errors());
            redirect('admin/tour/insert_foreign');
        }
    

    Please make this change and show us the output.

    0 讨论(0)
提交回复
热议问题