CodeIgniter Forums
Only upload if user set a value - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Only upload if user set a value (/showthread.php?tid=34823)



Only upload if user set a value - El Forum - 10-10-2010

[eluser]Corbee[/eluser]
Hi,

I'm doing a file upload, and I needed it to only upload if the user has set a value to the image.

I tried using

Code:
if ($this->input->post('logo'))
            {
                $config['upload_path'] = './images/CarLogo';
                $config['allowed_types'] = 'gif|jpg|png';
                $config['max_size']    = '100';
                $config['max_width']  = '180';
                $config['max_height']  = '180';
                $config['overwrite'] = 'TRUE';
                $this->load->library('upload', $config);
            
                if(!$this->upload->do_upload('logo'))
                {
                    $this->upload->display_errors();
                    exit();
                }
                    $logo = $this->upload->data();
                    
                if ($logo['file_name'])
                {
                    $logo = $logo['file_name'];
                    $this->MManage->updatelogo($logo);
                }
            }

but if you do an echo on the $this->input->post('logo');
you'll realize that it is always blank whether user sets it or not.

What's wrong with my code?

Thanks


Only upload if user set a value - El Forum - 10-10-2010

[eluser]techgnome[/eluser]
Does using isset work?

Code:
if (isset($this->input->post('logo')))

-tg


Only upload if user set a value - El Forum - 10-11-2010

[eluser]LuckyFella73[/eluser]
You want to check if a FILE was selected so you have to check
the file array - for example:
Code:
<?php
if (isset($_FILES['logo']['name']) AND strlen($_FILES['logo']['name'])>0){
// upload here
}
?>



Only upload if user set a value - El Forum - 10-12-2010

[eluser]Corbee[/eluser]
Thanks, $_FILES works.