Codeigniter 2.1 - 图像名称在上传时无法正常工作

时间:2013-01-24 15:51:45

标签: php codeigniter codeigniter-2

我正在使用此功能上传图像,除了一部分外,它正在工作。如果有多个图像可供上传,则所有图像都会从第一个图像中获取其名称(覆盖设置为关闭,因此CI在名称末尾添加数字)。我怎么解决这个问题?

function img_upload($folder) {
    $this->path = './public/img/' . $folder;
    $imgs = array();
    $count = 0;
    foreach($_FILES as $key => $value):
        $img_name = is_array($value['name']) ? $value['name'][$count] : $value['name'];
        $img_name = $this->char_replace($img_name, '_');
        $count++;
        $config = array(
            'allowed_types' => 'jpg|jpeg|png|gif',
            'upload_path' => $this->path,
            'file_name' => $img_name
        );
        $this->CI->load->library('image_lib');
        $this->CI->image_lib->clear();
        $this->CI->load->library('upload', $config);
        if($key != 'logo'):
            if (!$this->CI->upload->do_upload($key)) {
            } else {
                $image = $this->CI->upload->data();
                $imgs[] = $image['file_name'];
            }
        endif;
    endforeach;

    if(empty($imgs)):
        return FALSE;
    else:
        return implode(',', $imgs);
    endif;
}

功能 char_replace 正常运行。

function char_replace($text, $rep_simbol = " ")
{
    $char = array('!', '&', '?', '/', '/\/', ':', ';', '#', '<', '>', '=', '^', '@', '~', '`', '[', ']', '{', '}');
    return $name = str_replace($char, $rep_simbol, $text);
}

1 个答案:

答案 0 :(得分:1)

$this->CI->upload->do_upload($key)期望$_FILES['key']只包含一个文件。

您可以做的是,制作$_FILES的副本,循环浏览它,并为每个文件设置$_FILES['key']的值。

function img_upload($folder) {
    $this->path = './public/img/' . $folder;
    $imgs = array();

    // Copy of $_FILES
    $thisFiles = $_FILES;

    // Loop through copy of $_FILES
    foreach($theFiles as $key => &$value){
        // Create the $_FILES array for each individual file,
        // so that do_upload can read it correctly
        if(!is_array($value['name'])){
            // If it's not an array, make it one,
            // this will make our future code easier
            foreach($value as $kv => &$val){
                $val = array($val);
            }
        }

        // Loop through each file and upload each one
        foreach($value['name'] as $count=>$img_name){
            $img_name = $this->char_replace($img_name, '_');

            foreach($_FILES[$key] as $k => &$v){
                // CodeIgniter will think this is the $_FILES array
                $v = $theFiles[$key][$k][$count];
            }

            $config = array(
                'allowed_types' => 'jpg|jpeg|png|gif',
                'upload_path' => $this->path,
                'file_name' => $img_name
            );

            $this->CI->load->library('image_lib');
            $this->CI->image_lib->clear();
            $this->CI->load->library('upload', $config);

            if($key != 'logo'){
                if (!$this->CI->upload->do_upload($key)) {
                }
                else {
                    $image = $this->CI->upload->data();
                    $imgs[] = $image['file_name'];
                }
            }
        }
    }

    return !empty($imgs) ? implode(',', $imgs) : FALSE;
}

注意:这是未经测试的