• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Check to see if there is a file to upload

#1
[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!

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

#3
[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.

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

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

#5
[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.


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.