Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/276.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 如何将包含的$文件转换为数组?_Php_Arrays_Codeigniter_Foreach - Fatal编程技术网

Php 如何将包含的$文件转换为数组?

Php 如何将包含的$文件转换为数组?,php,arrays,codeigniter,foreach,Php,Arrays,Codeigniter,Foreach,如何解决此错误 A PHP Error was encountered Severity: Warning Message: Invalid argument supplied for foreach() Filename: admin/tour.php Line Number: 81 这是第81行: $files = $this->multi_upload->go_upload(); var_dump($files); $images = arr

如何解决此错误

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

$files = $this->multi_upload->go_upload();
        var_dump($files);
        $images = array();     
        foreach ($files as $img) {   //line 81
          $images[] = $img['file'];
        }
这是顶级代码中的my$文件:

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;
        }

        // Is the upload path valid?
        if ( ! $CI->upload->validate_upload_path())
        {
            // errors will already be set by validate_upload_path() so just return FALSE
            return FALSE;
        }

        for ($i=0; $i < $num_files; $i++) {

//            $fname = $_FILES[$field]['name'][$i];
//            echo "$fname\n\n<br><br>\n\n";

            $error_hold[$i] = FALSE;

            // Was the file able to be uploaded? If not, determine the reason why.
            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;
            }

            // Set the uploaded data as class variables
            $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]);

            // Convert the file size to kilobytes
            if ($CI->upload->file_size > 0)
            {
                $CI->upload->file_size = round($CI->upload->file_size/1024, 2);
            }

            // Is the file type allowed to be uploaded?
            if ( ! $CI->upload->is_allowed_filetype())
            {
                $error_hold[$i] = 'upload_invalid_filetype';
            }

            // Is the file size within the allowed maximum?
            if ( ! $CI->upload->is_allowed_filesize())
            {
                $error_hold[$i] = 'upload_invalid_filesize';
            }

            // Are the image dimensions within the allowed size?
            // Note: This can fail if the server has an open_basdir restriction.
            if ( ! $CI->upload->is_allowed_dimensions())
            {
                $error_hold[$i] = 'upload_invalid_dimensions';
            }

            // Sanitize the file name for security
            $CI->upload->file_name = $CI->upload->clean_file_name($CI->upload->file_name);

            // Remove white spaces in the name
            if ($CI->upload->remove_spaces == TRUE)
            {
                $CI->upload->file_name = preg_replace("/\s+/", "_", $CI->upload->file_name);
            }

            /*
             * Validate the file name
             * This function appends an number onto the end of
             * the file if one with the same name already exists.
             * If it returns false there was a problem.
             */
            $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;
                }
            }

            /*
             * Move the file to the final destination
             * To deal with different server configurations
             * we'll attempt to use copy() first.  If that fails
             * we'll use move_uploaded_file().  One of the two should
             * reliably work in most environments
             */
            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';
                }
            }

            /*
             * Run the file through the XSS hacking filter
             * This helps prevent malicious code from being
             * embedded within a file.  Scripts can easily
             * be disguised as images or other file types.
             */
            if ($CI->upload->xss_clean == TRUE)
            {
                $CI->upload->do_xss_clean();
            }

            if ($error_hold[$i]) {
                $error_upload = TRUE;

//                echo $error_hold[$i];
            } 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,
                            );
                }
            }

// For debugging
/*            
            if (strlen($error_hold[$i]) > 1) {
                    print_r($error_hold);
            }
*/            
        } // end for loop

// Add error display for individual files        
        if ($error_upload) {
            $this->set_error($error_hold);
            return FALSE;
        } else {
            return $file_list;
        }    
    }

简单的答案是$files不是数组。foreach只在数组上工作。我们不知道这个值是多少,但无论如何,它是不对的。您必须查看go_upload函数,看看它为什么没有达到预期效果。

您确定得到了一个数组吗?是看几回假。所以你应该检查一下

if ($files !== FALSE)
或者,如果返回false,则将其强制转换为空数组

$files = (array) $this->multi_upload->go_upload();

实际上,这样做你会得到你想要的文件或值

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

此行->$files=$This->multi_upload->go_upload;可能来自表单中的文件对象,对吗?然后它必须像这个文件[]一样命名,而不仅仅是一个文件。您需要明确它是一个数组。否则什么也不会发生。

也请避免看到此警告消息。您应该计算阵列:

if (count($files) > 0)
{
    foreach ($files as $img) {   //line 81
        $images[] = $img['file'];
    }
}

关于“为foreach提供的参数无效”错误,这可能是因为$files不是数组或类似的数组。最好的方法是:

    $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');
    }

请进行此更改并向我们显示输出。

我想我只想分享我如何在CI中处理多个上载,这与您的方法有点不同:

表单如下所示:

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);
    }
}    
public function do_upload($field = 'userfile')
改为

is_uploaded_file($_FILES[$field]['tmp_name'])
此函数中第200行以上的任何行,都必须将[$i]添加到$\u FILES变量的末尾

例如:

is_uploaded_file($_FILES[$field]['tmp_name'][$i])
改为:


有9行需要更新。

在设置$files之后,您可能希望var_转储$files。它可能不是一个数组或类似的数组。您应该先保存而不保存BOM表。然后,var_转储$files并查看它是否是一个数组,它应该是$files的样子。错误表明它不是数组打印r$文件时得到了什么-var_dump$文件的输出是什么?“只有在那之后我们才能帮助你。我不明白,请再打电话来。”马丁$文件不是数组。我不知道为什么,但是$this->multi_upload->go_upload返回的不是数组的东西。没有任何额外的信息,我只能说这些了。您必须查看该函数的源代码才能了解更多信息。您得到了什么输出?即使$files为空,也应该显示“files:”。输出是相同的错误。请转到这个地址:,在这里帮我。我把完整的代码…谢谢,为什么输出多次信息文件重复,我只上传两次与两个文件banana63.gif,untitle-159.gif。如果我只想输出名称文件没有所有的信息,它是如何?比如:banana63.gif,Untitled-159.gif
is_uploaded_file($_FILES[$field]['tmp_name'])
is_uploaded_file($_FILES[$field]['tmp_name'][$i])