Counting the number of uploaded files

13,273

You can check the number of items in your $_FILES variable using for example:

$total = count($_FILES['your_variable_array_in_html']['tmp_name']);

You need to do that before the:

$this->upload->do_multi_upload("files");

line.

As you have already noticed, $_FILES only contains one variable - an array - containing arrays of the different sections, tmp_name, name, error, etc. Check the manual for more details.

Share:
13,273
Frederik
Author by

Frederik

Experienced in Cisco routing/switching, Mikrotik routers, Windows Server 2008-2016, Debian/CentOS Linux, puppet and a whole lot more Programming experience includes PHP, C#.NET and I've been touching most other languages more or less

Updated on June 25, 2022

Comments

  • Frederik
    Frederik almost 2 years

    I am working on my CodeIgniter project, and it is so far working very well.

    However, I need some way to count the number of uploaded files, since I want to limit it in some cases (but not all).

    How can I do that? I tried count($_FILES) but that gave me nothing usable.

    I also tried a bunch of other things, but neither gave me the information I need.

    The upload form is a multiple file upload, and I am using this library to handle multiple uploads.

    The upload function without the counting looks like this:

    function do_upload()
    {
        $setid = $this->input->post('imageset');
            $this->load->library('upload');
            $this->load->library('image_lib');
            $this->upload->initialize(array(
                    "upload_path"   => "./photos/",
                    "allowed_types" => "jpg|jpeg|png|gif",
                    "encrypt_name" => TRUE
            ));
            try {
                $this->upload->do_multi_upload("files");
                $images = $this->upload->get_multi_upload_data();
                $config = array(
                        'image_library'  => 'gd2',
                        'create_thumb'   => TRUE,
                        'maintain_ratio' => TRUE,
                        'width'          => '145',
                        'height'         => '145'
                );
                foreach ($images as $image)
                {
                    $config['source_image'] = $image['full_path'];
                    $this->image_lib->initialize($config);
                    $this->image_lib->resize();
                    $this->manage_model->insertimage($image['file_name'],$image['orig_name'],$image['file_size'],$image['file_type'],$setid);
                }
                $this->session->set_flashdata('success','Billederne er nu blevet uploadet.');
            } catch (Exception $e) {
                $this->session->set_flashdata('error', $e);
            }
            redirect('manage/images','refresh');
    }
    

    Any help is very appreciated.

  • Frederik
    Frederik almost 11 years
    Thanks! Exactly what I was looking for.
  • Norman
    Norman over 9 years
    @jeroen Any idea how to get the count of the number of files a user has actually uploaded. Right now, my form has 5 file fields, and using this method gives me 5 even when the user uploads only 2 files.
  • Phoenix
    Phoenix almost 8 years
    count shows always 1 And some Time It shows 5 when i upload file