Welcome Guest, Not a member yet? Register   Sign In
File Uploading Problem - Duplicate File Extensions
#2

[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:
<?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;
        }
    }    
}


Messages In This Thread
File Uploading Problem - Duplicate File Extensions - by El Forum - 10-09-2010, 12:33 AM
File Uploading Problem - Duplicate File Extensions - by El Forum - 10-09-2010, 12:51 PM



Theme © iAndrew 2016 - Forum software by © MyBB