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']1 つのファイルのみが含まれることを想定しています。

できることは、 のコピーを作成し$_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 に答える