6

I got some problems with changing name of uploaded file:

$config = array(
        'allowed_types' => 'mp3',
        'file_name' => $fulltitle,       // Lets say we've entered 'a.s.d.f.mp3'
        'upload_path' => './music/'
    );
$this->load->library('upload', $config);        
$this->upload->do_upload(); 

But, when I check my filename it shows me

a.s_.d_.f_.mp3

Why CodeIgniter add underscore before every dot after first one? How I can disable this? Thank you.

ADDED

Well I found solution. system->libraries->Upload.php file.

Line 994, _prep_filename() function.

        $parts      = explode('.', $filename);
    $ext        = array_pop($parts);
    $filename   = array_shift($parts);

    foreach ($parts as $part)
    {
        if ( ! in_array(strtolower($part), $this->allowed_types) OR $this->mimes_types(strtolower($part)) === FALSE)
        {
            $filename .= '.'.$part.'_'; // Line 994
        }
        else
        {
            $filename .= '.'.$part;
        }
    }
Bhargav Rao
  • 50,140
  • 28
  • 121
  • 140
CyberLom
  • 61
  • 1
  • 3
  • I was just looking through and found that as well. You got to it before me. :) – Fuseblown Apr 20 '11 at 10:14
  • Is there a way to get this effect without directly modifying the system files ? – maan81 Dec 07 '12 at 08:24
  • I'm trying to use dots in my file name, but no way, no how with CodeIgniter. So when I specify something like `abcdefg.room.001.jpg` it will automatically rename the file `abcdefg_room_001.jpg`. I would prefer to use the dots, because I want to do something like `explode(".","abcdefg.room.001.jpg");` Otherwise, I have to use more complicated string manipulation using strpos and substr. – TARKUS Apr 05 '16 at 18:57
  • Nice find, @CyberLom ! – TARKUS Apr 05 '16 at 19:03

1 Answers1

4

Try adding 'remove_spaces' => FALSE to your config array and see if that takes care of the problem. This is set to TRUE by default, but it should only be replacing spaces with underscores. It could be a CI bug with the file uploading class.

Fuseblown
  • 771
  • 4
  • 14
  • 1
    Thanks for the configuration fix, though, a similar issue has occurred with me today and I have used regular expressions to remove the dots from the file name. More details on https://stackoverflow.com/questions/75309274/image-mime-type-and-extension-is-correct-but-get-failed-upload – Asad Ali Feb 09 '23 at 17:51