2013-11-23 63 views
0

我有以下情況: 當用戶單擊時,我需要一次上傳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被稱爲的結構是這樣的:

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] => 
    ) 

) 

回答

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']; 
     } 
    } 
} 
+0

但我怎麼會在這個do_mysql_load_data_infile中標識文件的名稱? – Periback

+0

看到我上面的例子。而不是隻存儲計數,將數據存儲在數組中並檢查數組的長度。 –

+0

謝謝米勒! :D – Periback

相關問題