Sasha
Sasha

Reputation: 8705

Codeigniter 2.1 - character replace

I have a felling that something is trolling me. I have this two functions. Purpose is to replace characters, but something is not working. Name of the file is changed, but only space (" ") is changed to _ character (there are no more str_replace functions). What is wrong?

EDIT **char_replace** is found in separate library file which is not extending controller. I am using char_replace function to replace data coming from input type=text and it is working (function is being called from different controller).

function img_upload($folder) {
        $this->path = './public/img/' . $folder;
        $imgs = array();
        $config = array(
            'allowed_types' => 'jpg|jpeg|png|gif',
            'upload_path' => $this->path
        );

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

        foreach ($_FILES as $key => $value) {
            $img_name = $this->char_replace($key->name, '_');
            $config['file_name'] = $img_name;
          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;
        }

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

And this one:

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

Upvotes: 1

Views: 1957

Answers (2)

air4x
air4x

Reputation: 5683

foreach ($_FILES as $key => $value) {
  $img_name = $this->char_replace($key->name, '_');
  ...

Here $key->name will be undefined and so char_replace will return an empty string. Since file_name is empty, Codeigniter Upload library will fallback to its _prep_filename method.

Use $value['name'] instead of $key->name.

If you are uploading multiple files with the file field having the same name

$count = 0;
foreach ($_FILES as $filename => $values) {
  $img_name = is_array($values['name']) ? $values['name'][$count] : $values['name'];
  $img_name = $this->char_replace($img_name, '_');
  $count++;

Upvotes: 1

wallyk
wallyk

Reputation: 57784

I thought the replacement has to have at least as many elements as the search:

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

Upvotes: 0

Related Questions