Codeigniter 2.1 - 文件上传,字符替换

时间:2012-11-05 15:48:53

标签: php codeigniter codeigniter-2

我有这个功能:

这个用于图片上传:

   function img_upload($folder) {
            $this->path = './public/img/' . $folder;
            $imgs = array();
            $count = 0;
            foreach ($_FILES as $key => $value) {
                $img_name = $this->char_replace($value['name'][$count], '_');
                $count++;
                $config = array(
                'allowed_types' => 'jpg|jpeg|png|gif',
                'upload_path' => $this->path,
                'file_name' => $img_name
            );
                $this->CI->load->library('upload', $config);
              if($key != 'logo') :
                  if (!$this->CI->upload->do_upload($key)) {
                } else {
                    $q = $this->CI->upload->data();
                    $config['image_library'] = 'gd2';
                    $config['source_image'] = $this->path . '/' . $q['file_name'];
                    $config['new_image'] = $this->path . '/thumbs';
                    $config['create_thumb'] = FALSE;
                    $config['maintain_ratio'] = TRUE;
                    $config['width'] = 128;
                    $config['height'] = 128;

                    $this->CI->load->library('image_lib');
                    $this->CI->image_lib->clear();
                    $this->CI->image_lib->initialize($config);
                    $this->CI->image_lib->resize();
                    array_push($imgs, $q['file_name']);
                }
              endif;
            }

这个用于替换字符:

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

上传效果很好,但字符替换效果不是很好。它只使用正在上传的文件名中的第一个字母,其余部分被截断了吗?这有什么不对?

1 个答案:

答案 0 :(得分:0)

第二个参数应该是数组:

function char_replace($text, $rep_simbol = " ")
    {
        $char = array('!', '&', '?', '/', '/\/', ':', ';', '#', '<', '>', '=', '^', '@', '~', '`', '[', ']', '{', '}');
        $symbols = array();
        foreach ($char as $val)
        $symbols[] = " ";//or empty string, whatever you want
        return $name = str_replace($char, $symbols, $text);
    }