CodeIgniter Forums
File Uploading Problem - Duplicate File Extensions - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: File Uploading Problem - Duplicate File Extensions (/thread-34776.html)



File Uploading Problem - Duplicate File Extensions - El Forum - 10-09-2010

[eluser]azavala[/eluser]
I whipped up a quick library to handle multiple file uploads for a small project I'm dabbling with. Basically, I'm going to mostly make use of this library from within models that handle calling the file upload library and also adding some stuff to a database. In the code below, I have a random string generated to use for a new file name. An underscore and number get appended to it in the event that there is more than one file being uploaded by the user for use with a piece of content.

The problem I'm having right now is that a duplicate file extension will appear in the uploaded file's name after the fact. However, in the library function's return data (see code below) after uploading, the file name is how it should be. That's good since the model uses that data for the database (image file name). Obviously, it's not good that the actual uploaded file has 2 file extensions (e.g. picture_name.jpg.jpg). I looked through these forums and found a similar problem that someone was having with the image manipulation class so I figure there is something wrong in my code causing the problem. I did add some stuff after the $config settings to try and get around this problem (kept it in the code below as well), but I'm still running into the same problem.

What am I missing here? Mind you, it's late and I'm still learning. Any help would be appreciated!

Code:
Code:
<?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Uploader
{
    public $CI;
    private $upload_path;
    
    public function __construct()
    {        
        if (!isset($this->CI))
        {
            $this->CI =& get_instance();
        }
    }
    
    public function setPath($path)
    {
        $this->upload_path = $path;
    }
    
    public function upload_images($field_name, $max_file_size = '3584', $max_width = '800', $max_height = '1000')
    {        
        $upload_response = array();
        
        if ($this->upload_path == NULL)
        {
            // No upload path was set!
            return FALSE;
        }
        else
        {
            $new_name = random_string('alnum', 12);
    
             for ($i = 0; $i < count($_FILES[$field_name]['name']); $i++)
             {                        
                   $_FILES['userfile']['name']        =     $_FILES[$field_name]['name'][$i];
                   $_FILES['userfile']['type']        =     $_FILES[$field_name]['type'][$i];
                   $_FILES['userfile']['tmp_name'] =     $_FILES[$field_name]['tmp_name'][$i];
                   $_FILES['userfile']['error']    =     $_FILES[$field_name]['error'][$i];
                   $_FILES['userfile']['size']        =     $_FILES[$field_name]['size'][$i];
                                
                   $config['file_name']         =     $new_name . '_' . $i;
                $config['upload_path']       =     $this->upload_path;
                $config['allowed_types']     =     'jpg|jpeg|gif|png';
                   $config['max_size']          =     $max_file_size;
                $config['max_width']        =    $max_width;
                $config['max_height']        =    $max_height;
                   $config['overwrite']        =    TRUE;
                                
                if (stristr($config['file_name'], '.') === FALSE)
                {
                    // Not found, need to add a file extension
                    $config['file_name'] = $config['file_name'] . '.' . pathinfo($_FILES[$field_name]['name'][$i], PATHINFO_EXTENSION);
                }
                elseif (substr_count($config['file_name'], '.') > 1)
                {
                    // has double extensions, fix it
                    switch (pathinfo($_FILES[$field_name]['name'][$i], PATHINFO_EXTENSION))
                    {
                        case 'jpg':
                            $config['file_name'] = str_replace('.jpg.jpg', '.jpg', $config['file_name']);
                            break;
                        case 'jpeg':
                            $config['file_name'] = str_replace('.jpeg.jpeg', '.jpeg', $config['file_name']);
                            break;
                        case 'gif':
                            $config['file_name'] = str_replace('.gif.gif', '.gif', $config['file_name']);
                            break;
                        case 'png':
                            $config['file_name'] = str_replace('.png.png', '.png', $config['file_name']);
                            break;
                    }
                }
                
                  // Load the upload library
                  $this->CI->load->library('upload');
                  $this->CI->upload->initialize($config);

                  if (!$this->CI->upload->do_upload())
                  {
                    $upload_response []= array('status' => FALSE, 'errors' => $this->CI->upload->display_errors());
                  }
                  else
                  {
                    $upload_response []= array('status' => TRUE, 'name' => $config['file_name']);
                  }
             }
                
            return $upload_response;
        }
    }    
}


Edit: Moved the upload library load to within the image upload function. Tried with and without the initialize function. No change in result. Sad


File Uploading Problem - Duplicate File Extensions - El Forum - 10-09-2010

[eluser]azavala[/eluser]
Took a fresh look at this today and still don't know what I'm doing wrong. I'm going to try changing some config items to see where that gets me.

EDIT: Think I might have figured it out. I moved the block of code that is supposed to fix the duplicate file extension problem in the later part of the entire function, where the file name is part of the return result, which is what is used by models as part of the data they put into the db. Final code looks like this:

Code:
&lt;?php if (!defined('BASEPATH')) exit('No direct script access allowed');

class Uploader
{
    public $CI;
    private $upload_path;
    
    public function __construct()
    {        
        if (!isset($this->CI))
        {
            $this->CI =& get_instance();
        }
    }
    
    public function setPath($path)
    {
        $this->upload_path = $path;
    }
    
    public function upload_images($field_name, $max_file_size = '3584', $max_width = '800', $max_height = '1000')
    {        
        $upload_response = array();
        
        if ($this->upload_path == NULL)
        {
            // No upload path was set!
            return FALSE;
        }
        else
        {
            $new_name = random_string('alnum', 12);
    
             for ($i = 0; $i < count($_FILES[$field_name]['name']); $i++)
             {                        
                   $_FILES['userfile']['name']        =     $_FILES[$field_name]['name'][$i];
                   $_FILES['userfile']['type']        =     $_FILES[$field_name]['type'][$i];
                   $_FILES['userfile']['tmp_name'] =     $_FILES[$field_name]['tmp_name'][$i];
                   $_FILES['userfile']['error']    =     $_FILES[$field_name]['error'][$i];
                   $_FILES['userfile']['size']        =     $_FILES[$field_name]['size'][$i];
                                
                   $config['file_name']         =     $new_name . '_' . $i;
                $config['upload_path']       =     $this->upload_path;
                $config['allowed_types']     =     'jpg|jpeg|gif|png';
                   $config['max_size']          =     $max_file_size;
                $config['max_width']        =    $max_width;
                $config['max_height']        =    $max_height;
                   $config['overwrite']        =    TRUE;
                                
                // Load the upload library
                $this->CI->load->library('upload');            
                  $this->CI->upload->initialize($config);

                  if (!$this->CI->upload->do_upload())
                  {
                    $upload_response []= array('status' => FALSE, 'data' => $this->CI->upload->display_errors());
                  }
                  else
                  {
                    // Fix the file name before adding to the return data
                    if (stristr($config['file_name'], '.') === FALSE)
                    {
                        // Not found, need to add a file extension
                        $config['file_name'] = $config['file_name'] . '.' . pathinfo($_FILES[$field_name]['name'][$i], PATHINFO_EXTENSION);
                    }
                    elseif (substr_count($config['file_name'], '.') > 1)
                    {
                        // has double extensions, fix it
                        switch (pathinfo($_FILES[$field_name]['name'][$i], PATHINFO_EXTENSION))
                        {
                            case 'jpg':
                                $config['file_name'] = str_replace('.jpg.jpg', '.jpg', $config['file_name']);
                                break;
                            case 'jpeg':
                                $config['file_name'] = str_replace('.jpeg.jpeg', '.jpeg', $config['file_name']);
                                break;
                            case 'gif':
                                $config['file_name'] = str_replace('.gif.gif', '.gif', $config['file_name']);
                                break;
                            case 'png':
                                $config['file_name'] = str_replace('.png.png', '.png', $config['file_name']);
                                break;
                        }
                    }
                    
                    $upload_response []= array('status' => TRUE, 'data' => $config['file_name'], 'path' => $config['upload_path']);
                  }
             }
                
            return $upload_response;
        }
    }    
}