【问题标题】:Store uploaded file data into array将上传的文件数据存储到数组中
【发布时间】:2013-11-23 17:52:36
【问题描述】:

我有以下情况: 当用户单击时,我需要一次上传 3 个文件。这是我认为的代码:

<?php echo form_open_multipart('uploads/do_upload', 'class="dropzone", id="dropzone-prod", data-nextFormExecDropzone="#dropzone-promo"');?>
   <div class="fallback">
     <input type="file" name="userfile" size="20" />
   </div>
</form>
<?php echo form_open_multipart('uploads/do_upload', 'class="dropzone", id="dropzone-promo", data-nextFormExecDropzone="#dropzone-plan"');?>
   <div class="fallback">
     <input type="file" name="userfile" size="20" />
   </div>
</form>
<?php echo form_open_multipart('uploads/do_upload', 'class="dropzone", id="dropzone-plan"');?>
   <div class="fallback">
     <input type="file" name="userfile" size="20" />
   </div>
</form>
<div class="col-lg-4">
    <button id="processQueue"  class="btn btn-primary" type="button"><i class="fa fa-upload"></i>Start Upload</button>
</div>

当用户点击按钮时,我有一个 javascript 代码,可以按照我想要的顺序发送每个表单。 在我的控制器中,我有以下方法(do_upload()):

function do_upload() {
    //$filePath = $this->config->item('base_current_upload_url');
    $filePath = APPPATH.'UPLOADS/';
    $filePathAfterUploaded = $this->config->item('base_uploaded_url');

    $config['upload_path'] = $filePath;
    $config['allowed_types'] = '*';
    $config['max_size'] = 1024 * 100;

    $this->load->library('upload', $config);

    //$this->load->library('csvreader');
    //$filePathAfterUploaded = $this->config->item('base_uploaded_url');
    //print_r($filePath); die();
    $basefilepath = APPPATH.'UPLOADS/';
    $this->load->model('uploads_m');

    if ( ! $this->upload->do_upload('file')) {
        $error = array('error' => $this->upload->display_errors());

        print_r($error);
    }
    else {
        $data = array('upload_data' => $this->upload->data());
        print_r($data); die();
    }

}

问题:

我需要上传 3 个文件。如果只缺少一个,我将无法继续。 我正在这样做,在每个文件的上传过程中都会调用函数do_upload,所以我需要找到一种方法来识别这3个文件是否已上传。 (之后,我会使用 mysql 'load data infile' 将这些文件中的数据加载到一些表中,但我只能在这三个都上传的情况下才能这样做。 你能帮我找到处理这种情况的方法吗?

每次调用do_uplaod时$data的结构是这样的:

Array
(
[upload_data] => Array
    (
        [file_name] => PROMOWEB111120131.txt
        [file_type] => text/plain
        [file_path] => C:/Program Files/EasyPHP-DevServer-13.1VC9/data/localweb/projects/integration/www/application/UPLOADS/
        [full_path] => C:/Program Files/EasyPHP-DevServer-13.1VC9/data/localweb/projects/integration/www/application/UPLOADS/PROMOWEB111120131.txt
        [raw_name] => PROMOWEB111120131
        [orig_name] => PROMOWEB11112013.txt
        [client_name] => PROMOWEB11112013.txt
        [file_ext] => .txt
        [file_size] => 2.67
        [is_image] => 
        [image_width] => 
        [image_height] => 
        [image_type] => 
        [image_size_str] => 
    )

)

【问题讨论】:

    标签: php codeigniter multifile-uploader dropzone.js


    【解决方案1】:

    如果do_upload是一个独立的函数,那么引入一个统计调用次数的静态变量。

    function do_upload () {
        static $count = 0;
        // The rest of your function goes here
        if (no_errors_occurred) {
            static::$count++;
        }
        if ($count == 3) {
            // This function has been called 3 times; trigger something here
        }
    }
    

    更好的是,如果它在课堂上......

    class MyClass {
        protected static $data = array ();
    
        // Other functions and properties
    
        public function do_upload () {
            // The rest of your function
            if (no_errors_occurred) {
                static::$data[] = array (
                    'upload_data' => array (
                        'file_name' => ...,
                        // Populate the data array
                    )
                );
            }
            $this->do_mysql_load_data_infile();
        }
    
        protected function do_mysql_load_data_infile () {
            if (count(static::$data) != 3) {
                return false;
            }
            // Do MySQL load data infile
            // Get information about file uploads by accessing the static::$data array
            foreach (static::$data as $file) {
                echo $file['upload_data']['file_name'];
            }
        }
    }
    

    【讨论】:

    • 但是我如何识别这个 do_mysql_load_data_infile 中的文件名?
    • 参见我上面的课堂示例。不仅仅是存储计数,而是将数据存储在数组中并检查数组的长度。
    猜你喜欢
    • 2012-04-22
    • 1970-01-01
    • 1970-01-01
    • 2015-03-03
    • 1970-01-01
    • 2021-03-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多