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

1 回答 1

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

注意:这是未经测试的。

于 2013-01-24T16:57:01.990 回答