如何转换$文件包含数组? [英] how can convert $files contain to array?

查看:109
本文介绍了如何转换$文件包含数组?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要如何解决这个问题?

 遇到了PHP的错误
严重性:警告
消息:的foreach提供了无效的参数()
文件名:管理/ tour.php
行号:81


这是行81:

  $文件= $这个 - > multi_upload-> go_upload();
        后续代码var_dump($文件);
        $图像=阵列();
        的foreach($文件,$ IMG){//行81
          $图像[] = $ IMG ['文件'];
        }

在顶部code这是我的$文件:

 函数go_upload($字段='的用户文件'){
        $ CI =&安培; get_instance();
        //为$ _FILES [$场]设置?如果没有,没有理由继续。
        如果(!使用isset($ _ FILES [$场] [名称] [0]))
        {
            $ Cl - > upload-> set_error('upload_no_file_selected');
            返回FALSE;
        }其他
        {
            $ NUM_FILES =计数($ _ FILES [$场] ['名'])-1;
            $ file_list中=阵列();
            $ error_hold =阵列();
            $ error_upload = FALSE;
        }        //是上传路径是否有效?
        如果($ CI->!upload-> validate_upload_path())
        {
            //错误将已经validate_upload_path设置()所以才返回FALSE
            返回FALSE;
        }        为($ I = 0; $ I< $ NUM_FILES; $ I ++){// $ FNAME = $ _FILES [$场] ['名'] [$ i];
//回声$ FNAME \\ n \\ n< BR>< BR> \\ n \\ n;            $ error_hold [$ i] = FALSE;            //能够将文件上传?如果没有,确定原因。
            如果(!is_uploaded_file($ _ FILES [$场] ['tmp_name的值'] [$ i]))
            {
                $误差=(!使用isset($ _ FILES [$场] ['错误'] [$ i]))? 4:$ _FILES [$场] ['错误'] [$ i];                开关($错误)
                {
                    案例1:// UPLOAD_ERR_INI_SIZE
                        $ error_hold [$ i] ='upload_file_exceeds_limit';
                        打破;
                    案例2:// UPLOAD_ERR_FORM_SIZE
                        $ error_hold [$ i] ='upload_file_exceeds_form_limit';
                        打破;
                    案例3:// UPLOAD_ERR_PARTIAL
                       $ error_hold [$ i] ='upload_file_partial';
                        打破;
                    壳4:// UPLOAD_ERR_NO_FILE
                       $ error_hold [$ i] ='upload_no_file_selected';
                        打破;
                    情况6:// UPLOAD_ERR_NO_TMP_DIR
                        $ error_hold [$ i] ='upload_no_temp_directory';
                        打破;
                    案例7:// UPLOAD_ERR_CANT_WRITE
                        $ error_hold [$ i] ='upload_unable_to_write_file';
                        打破;
                    案例8:// UPLOAD_ERR_EXTENSION
                        $ error_hold [$ i] ='upload_stopped_by_extension';
                        打破;
                    默认情况下:
                        $ error_hold [$ i] ='upload_no_file_selected';
                        打破;
                }                返回FALSE;
            }            //设置上传的数据作为类变量
            $ CI-> upload-> file_temp = $ _FILES [$场] ['tmp_name的值'] [$ i];
        $ CI-> upload-> FILE_NAME = $ _FILES [$场] ['名'] [$ i];
            $ CI-> upload-> FILE_SIZE = $ _FILES [$场] ['大小'] [$ i];
            $ CI-> upload-> FILE_TYPE = preg_replace(/^(.+?);.*$/,\\\\ 1,$ _FILES [$场] [型] [$一世]);
            $ CI-> upload-> FILE_TYPE =用strtolower($ CI-> upload-> FILE_TYPE);
            $ CI-> upload-> file_ext = $ CI-> upload-> get_extension($ _ FILES [$场] ['名'] [$ i]);            //将文件大小千字节
            如果($ Cl - > upload-> FILE_SIZE大于0)
            {
                $ Cl - > upload-> FILE_SIZE =圆($ Cl - > upload-> FILE_SIZE / 1024,2);
            }            //是允许的文件类型上传?
            如果($ CI->!upload-> is_allowed_filetype())
            {
                $ error_hold [$ i] ='upload_invalid_filetype';
            }            //在允许的最大范围内文件的大小?
            如果($ CI->!upload-> is_allowed_filesize())
            {
                $ error_hold [$ i] ='upload_invalid_filesize';
            }            //在允许的尺寸范围内图像尺寸是多少?
            //注意:如果服务器有一个open_basdir限制,这可能会失败。
            如果($ CI->!upload-> is_allowed_dimensions())
            {
                $ error_hold [$ i] ='upload_invalid_dimensions';
            }            //消毒安全的文件名
            $ CI-> upload-> FILE_NAME = $ CI-> upload-> clean_file_name($ CI-> upload-> FILE_NAME);            //名称中删除空格
            如果($ CI-> upload-> remove_spaces == TRUE)
            {
                $ CI-> upload-> FILE_NAME = preg_replace(/ \\ s + /,_,$ CI-> upload-> FILE_NAME);
            }            / *
             *验证文件名
             *此功能的追加数到年底
             *如果一个文件具有相同名称已经存在。
             *如果返回false有问题。
             * /
            $ CI-> upload-> orig_name = $ CI-> upload-> FILE_NAME;            如果($ CI-> upload->覆盖== FALSE)
            {
                $ CI-> upload-> FILE_NAME = $ CI-> upload-> set_filename($ CI-> upload-> upload_path,$ CI-> upload-> FILE_NAME);                如果($ CI-> upload-> FILE_NAME === FALSE)
                {
                    $ error_hold [$ i] = TRUE;
                }
            }            / *
             *将文件移动到最终目的地
             *要处理不同的服务器配置
             *我们会先尝试使用复制()。如果失败
             *我们将使用move_uploaded_file()以。一两个应
             *可靠在大多数环境下工作
             * /
            如果(@copy($ CI->!upload-> file_temp,$ CI-> upload-方式> upload_path $ CI-> upload-> FILE_NAME))
            {
                如果(@move_uploaded_file($ CI->!upload-> file_temp,$ CI-> upload-方式> upload_path $ CI-> upload-> FILE_NAME))
                {
                     $ error_hold [$ i] ='upload_destination_error';
                }
            }            / *
             *运行通过XSS过滤器盗号文件
             *这有助于prevent恶意code被
             *嵌入在文件中。脚本可以很容易
             *伪装成图片或其他文件类型。
             * /
            如果($ CI-> upload-> xss_clean == TRUE)
            {
                $ Cl - > upload-> do_xss_clean();
            }            如果($ error_hold [$ i]){
                $ error_upload = TRUE;//回声$ error_hold [$ i];
            }其他{
                如果($ imageVar = $这个 - > multiple_image_properties($ CI-> upload-方式> upload_path $ CI-> upload-> FILE_NAME)){                    $ file_list中[] =数组(
                            '名'=> $ CI-> upload-> FILE_NAME,
                            '文件'= GT; $ CI-> upload-方式> upload_path $ CI-> upload-> FILE_NAME,
                            '大小'=> $ CI-> upload-> FILE_SIZE,
                            '分机'=> $ CI-> upload-> file_ext,
                            IMAGE_TYPE'=> $ imageVar-> IMAGE_TYPE,
                            高度= GT; $ imageVar->的高度,
                            '宽度'=> $ imageVar->宽度
                            );
                }其他{
                    $ file_list中[] =数组(
                            '名'=> $ CI-> upload-> FILE_NAME,
                            '文件'= GT; $ CI-> upload-方式> upload_path $ CI-> upload-> FILE_NAME,
                            '大小'=> $ CI-> upload-> FILE_SIZE,
                            '型'=> $ CI-> upload-> FILE_TYPE,
                            '分机'=> $ CI-> upload-> file_ext,
                            );
                }
            }//为了调试
/ *
            如果(strlen的($ error_hold [$ i]于)→1){
                    的print_r($ error_hold);
            }
* /
        } //结束for循环//添加单个文件的错误显示
        如果($ error_upload){
            $这个 - > set_error($ error_hold);
            返回FALSE;
        }其他{
            返回$ file_list中;
        }
    }


解决方案

简单的答案是, $文件不是数组。 的foreach 仅适用于数组。我们不知道这个值是什么,但在任何情况下,这是不对的。你不得不看 go_upload 功能,看看它为什么没有做你所期望的。

How do I solve this error?

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

this is line 81:

$files = $this->multi_upload->go_upload();
        var_dump($files);
        $images = array();     
        foreach ($files as $img) {   //line 81
          $images[] = $img['file'];
        }

this my $files in top 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;
        }

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

解决方案

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.

这篇关于如何转换$文件包含数组?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆