计算上传文件的数量

时间:2013-05-11 21:33:20

标签: php codeigniter file-upload

我正在开发我的CodeIgniter项目,到目前为止工作得非常好。

但是,我需要一些方法来计算上传文件的数量,因为我想在某些情况下限制它(但不是全部)。

我该怎么做?我试过了count($_FILES),但这没有给我任何用处。

我也尝试了很多其他的东西,但都没有给我提供我需要的信息。

上传表单是一个多文件上传,我使用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');
}

非常感谢任何帮助。

2 个答案:

答案 0 :(得分:11)

您可以使用例如:

检查$_FILES变量中的项目数
$total = count($_FILES['your_variable_array_in_html']['tmp_name']);

您需要在:

之前执行此操作
$this->upload->do_multi_upload("files");

线。

正如您已经注意到的,$_FILES只包含一个变量 - 一个数组 - 包含不同部分的数组tmp_namenameerror等。 manual了解更多详情。

答案 1 :(得分:1)

如果要限制上传的文件。 你可以分两步来限制它们:

  1. 仅接受上传文件的指定数组名称。即文件[]

  2. 现在您可以轻松统计

  3. 上传的文件
      

    计数($ _ FILES [ '文件'] [ '名称']);

    字符串'name'可以替换为'tmp_name','error','size','type'

    您使用的库在首次执行时已将文件上载到您的上传路径中。所以你应该在上传之前手动检查它。

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

    希望这会有所帮助