Counting the number of uploaded files

2019-05-14 12:50发布

问题:

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.

回答1:

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.



回答2:

if you want to limit the files uploaded. you may limit them in two steps:

  1. Only accept a specified array name of the uploading files. i.e files[]

  2. Now you can easily count the files uploaded by

count($_FILES['files']['name']);

string 'name' can be replaced by 'tmp_name', 'error', 'size', 'type'

The library that you used have uploaded files into your upload path when it is first executed. so you should manually check it before you do upload.

  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
    ));
    if (isset($_FILES['files']['name'])) {
      $num = count($_FILES['files']['name']);
    }

        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');
      }

hope this helps