Welcome Guest, Not a member yet? Register   Sign In
File extension problem with upload
#1

[eluser]shinokada[/eluser]
I have the following controller to upload a image and thumb image.

However uploaded image file name, sample_image.jpg becomes sample_image.jpg.jpg, and .gif becomes .gif.gif etc.

How can I avoid this problem?

Code:
function addProduct(){
    $data = array(
        'name' => db_clean($_POST['name']),
        'shortdesc' => db_clean($_POST['shortdesc']),
        'longdesc' => db_clean($_POST['longdesc'],5000),
        'status' => db_clean($_POST['status'],8),
        'class' => db_clean($_POST['class'],30),
        'grouping' => db_clean($_POST['grouping'],16),
        'category_id' => id_clean($_POST['category_id']),
        'featured' => db_clean($_POST['featured'],5),
        'price' => db_clean($_POST['price'],16)
    
    );
    
    if ($_FILES){
        $config['upload_path'] = './images/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '200';
        $config['remove_spaces'] = true;
        $config['overwrite'] = false;
        $config['max_width']  = '0';
        $config['max_height']  = '0';
        $this->load->library('upload', $config);    
        if (strlen($_FILES['image']['name'])){
            if(!$this->upload->do_upload('image')){
                $this->upload->display_errors();
                exit();
            }
            $image = $this->upload->data();
            if ($image['file_name']){
                $data['image'] = "images/".$image['file_name'];
            }
        }
        if (strlen($_FILES['thumbnail']['name'])){
            if(!$this->upload->do_upload('thumbnail')){
                $this->upload->display_errors();
                exit();
            }
            $thumb = $this->upload->data();
            if ($thumb['file_name']){
                $data['thumbnail'] = "images/".$thumb['file_name'];
            }
        }
    }
    $this->db->insert('omc_products', $data);
...
...
#2

[eluser]pistolPete[/eluser]
I guess you need to initialize the upload library before the second do_upload() call:
Code:
$config['upload_path'] = './images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '200';
$config['remove_spaces'] = true;
$config['overwrite'] = false;
$config['max_width']  = '0';
$config['max_height']  = '0';
$this->load->library('upload', $config);

...

$this->upload->do_upload('image');

...

$this->upload->initialize($config);

...

$this->upload->do_upload('thumbnail');
#3

[eluser]shinokada[/eluser]
Thanks. It works now.
#4

[eluser]maria clara[/eluser]
@ shinokada,

may i ask a question?? what does your "db_clean" stands for?? because i have this script.
Code:
$fields = array(
                    'module_name'=>db_clean($_POST['module_name'],200),
                    'slug'=>db_clean($_POST['slug'],200),
                    'icon'=>db_clean($_POST['icon'],50),  
                    'file_name'=>db_clean($_POST['file_name'],100),          
                    'report_file_name'=>db_clean($_POST['report_file_name'],50),
                    'sql_select'=>db_clean($_POST['sql_select']),
                    'sql_filter'=>db_clean($_POST['sql_filter']),
                    'sql_order'=>db_clean($_POST['sql_order'])
                
                );

and my console shows me this error:
Quote:<br />
<b>Fatal error</b>: Call to undefined function db_clean() in <b>C:\xampp\htdocs\comunionerp\system\application\modules\at_reports\controllers\at_reports.php</b> on line <b>96</b><br />

please i need help..

thanks,
maria
#5

[eluser]shinokada[/eluser]
I have a helper with the following code. I autoload it.
Code:
function id_clean($id,$size=11){
    return intval(substr($id,0,$size));
}

function db_clean($string,$size=255){
    return xss_clean(substr($string,0,$size));
}
#6

[eluser]maria clara[/eluser]
hi, i have now this new code..
Code:
$this->load->helper('MY_security_helper');
                
                          
                $fields = array(
                    'module_name'=>$_POST['module_name'],
                    'slug'=>substr($_POST['slug'],0,200),
                    'icon'=>$_FILES['file_name'],        
                    'report_file_name'=>$_POST['report_file_name'],
                    'sql_select'=>$_POST['sql_select'],
                    'sql_filter'=>$_POST['sql_filter'],
                    'sql_order'=>$_POST['sql_order']
                
                );
                
                
                if ($_FILES){
                    $config['upload_path'] = './reports/module/';
                    $config['allowed_types'] = 'gif|jpg|png';
                    $config['max_size']    = '100';
                    $config['max_width']  = '1024';
                    $config['max_height']  = '768';
                    $config['remove_spaces'] = true;
                    $config['overwrite'] = false;
                    
                    $this->load->library('upload', $config);    
                    if (strlen($_FILES['icon']['file_name'])){
                        if(!$this->upload->do_upload('file_name')){
                            $this->upload->display_errors();
                            exit();
                        }
                        $image = $this->upload->data();
                        if ($image['file_name']){
                            $fields['icon'] = "reports/module/".$image['file_name'];
                        }
                    }
                    
                    $this->upload->initialize($config);
                    
        
                }
                $item = $this->input->post("item");
                $rows = $this->input->post("row0");
                            
                                        
                $data['item'] = $this->Reports->save($fields,$item,$rows);    
    
                                            
                $c .= 'Successfully saved item';

but my console shows me an error regarding this line:
'icon'=>$_FILES['file_name'], it says that file_name is NULL..
#7

[eluser]shinokada[/eluser]
Comment it out and see what happens.
#8

[eluser]maria clara[/eluser]
i already have tried that and it shows me this error.
Quote:<b>Fatal error</b>: ob_start() [&lt;a href='ref.outcontrol'&gt;ref.outcontrol&lt;/a&gt;]: Cannot use output buffering in output buffering display handlers in <b>C:\xampp\htdocs\comunionerp\system\libraries\Exceptions.php</b> on line <b>160</b>
#9

[eluser]shinokada[/eluser]
comment out this line as well.

$this->upload->initialize($config);

like this.
Code:
if ($image['file_name']){
                            $fields['icon'] = "reports/module/".$image['file_name'];
                        }
                    }
                    
          //          $this->upload->initialize($config);
                    
        
                }
                $item = $this->input->post("item");
                $rows = $this->input->post("row0");
#10

[eluser]maria clara[/eluser]
i did what you said, but still it shows me the same error. is it the $_POST that is the root of the ob_start() error??




Theme © iAndrew 2016 - Forum software by © MyBB