CodeIgniter Forums
Check to see if there is a file to upload - 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: Check to see if there is a file to upload (/showthread.php?tid=25258)



Check to see if there is a file to upload - El Forum - 12-06-2009

[eluser]jake142[/eluser]
Hi,

Im new to codeigniter (and new to php). How can I check if there is a file to upload, befor I do the actual upload? The $this->input->post('userfile')!="" doesn't work.

Code:
if($this->input->post('userfile')!="")
        {
            if ( ! $this->upload->do_upload())
            {
                $error = array('error' => $this->upload->display_errors());
                $this->load->view('header');
                $this->load->view('ads/error', $error);
                $this->load->view('right_side');
                $this->load->view('footer');
            }
            else
            {
                $data = array('upload_data' => $this->upload->data());
                $upload_data = $data['upload_data'];

                $config['image_library'] = 'gd2';
                $config['source_image'] = $upload_data['full_path'];

                $config['maintain_ratio'] = TRUE;
                $config['width'] = 300;
                $config['height'] = 300;
                $this->load->library('image_lib', $config);
                $this->image_lib->resize();
                $this->image_lib->clear();
                $imagename = $upload_data['file_name'];
                $contenttype = $upload_data['file_type'];
            }
        }
        else
        {
            //Just store some information in db and use default file (always uploaded)
        }

Any help is very appritiated.

Thanks!


Check to see if there is a file to upload - El Forum - 12-06-2009

[eluser]JoostV[/eluser]
Code:
if (isset($_FILES['userfile']['name']) && $_FILES['userfile']['name'] != "none") {
    // File was posted
}



Check to see if there is a file to upload - El Forum - 12-06-2009

[eluser]jake142[/eluser]
Hi,

It doesn't work for me. $_FILES['userfile']['name'] is always '0' no matter if I have uploaded a file or not.

I use <?php echo form_open_multipart('ads/do_save');?> in my view to post the uploaded file.

<input type="file" id="userfile" name="userfile" size="29" /> is the file upload element.

Any ideas?

Thanks.


Check to see if there is a file to upload - El Forum - 12-06-2009

[eluser]umefarooq[/eluser]
best way is to check is

Code:
if($_FILES['upload']['error'] == 0){
then upload file.
}



Check to see if there is a file to upload - El Forum - 12-06-2009

[eluser]jake142[/eluser]
Hi,

Thanks everyone. I need to check that the user really uploaded a file before I upload it. Using:

$_FILES['userfile']['size']!='0'

solved my problem.

Thanks.