2017-02-22 13 views
-1

複数の入力を持つ複数のファイルをcodeigniterにアップロードする方法。以下は私のコードです。私は多くのファイルを追加したいが、ファイルの入力は異なっている。codeigniterに複数の入力を含む複数のファイルをアップロードする方法

if(!empty($_FILES['countryfile']['name'])) 
     { 
     $filesCount = count($_FILES['countryfile']['name']); 

       for($i = 0; $i < $filesCount; $i++) 
        { 
         $imgFile=$_FILES['countryfile']['name'][$i]; 
         $tmp_dir=$_FILES['countryfile']['tmp_name'][$i]; 
         $imgSize=$_FILES['countryfile']['size'][$i]; 
         //$upload_dir='../uploads/dish_images'; 
         $imgExt=strtolower(pathinfo($imgFile,PATHINFO_EXTENSION)); 
         $valid_extensions = array('jpeg', 'jpg', 'png', 'gif','pdf'); 
         $image=rand(1000,10000).".".$imgExt; 

         $config['upload_path'] = '../admin/upload_doc'; 
         //$config['upload_path'] = 'http://teq-staging.com/maswad-phase2/admin/uploads/dish_images/'; 
         $config['allowed_types'] = 'jpg|jpeg|png|gif|pdf'; 
         $config['file_name'] = $_FILES['countryfile']['name']; 
         $upload_dir=$config['upload_path']; 
         //$config['file_name']=$image; 
         $upload_dir=$config['upload_path'];  


         $this->upload->initialize($config); 
         if($this->upload->do_upload('countryfile')){ 
          $upload_data = $this->upload->data(); 


        } 
       } 

答えて

0

アップロードコントローラ

<?php echo form_open_multipart('upload'); ?> 
<p> 
    <?php echo form_label('File 1', 'userfile') ?> 
    <?php echo form_upload('userfile') ?> 
</p> 
<p> 
    <?php echo form_label('File 2', 'userfile1') ?> 
    <?php echo form_upload('userfile1') ?> 
</p> 
<p><?php echo form_submit('submit', 'Upload them files!') ?></p> 
<?php form_close() ?> 

form.php

function index() 
{ 
    // Has the form been posted? 
    if (isset($_POST['submit'])) 
    { 
     // Load the library - no config specified here 
     $this->load->library('upload'); 

     // Check if there was a file uploaded - there are other ways to 
     // check this such as checking the 'error' for the file - if error 
     // is 0, you are good to code 
     if (!empty($_FILES['userfile']['name'])) 
     { 
      // Specify configuration for File 1 
      $config['upload_path'] = 'uploads/'; 
      $config['allowed_types'] = 'gif|jpg|png'; 
      $config['max_size'] = '100'; 
      $config['max_width'] = '1024'; 
      $config['max_height'] = '768';  

      // Initialize config for File 1 
      $this->upload->initialize($config); 

      // Upload file 1 
      if ($this->upload->do_upload('userfile')) 
      { 
       $data = $this->upload->data(); 
      } 
      else 
      { 
       echo $this->upload->display_errors(); 
      } 

     } 

     // Do we have a second file? 
     if (!empty($_FILES['userfile1']['name'])) 
     { 
      // Config for File 2 - can be completely different to file 1's config 
      // or if you want to stick with config for file 1, do nothing! 
      $config['upload_path'] = 'uploads/dir2/'; 
      $config['allowed_types'] = 'gif|jpg|png'; 
      $config['max_size'] = '100'; 
      $config['max_width'] = '1024'; 
      $config['max_height'] = '768'; 

      // Initialize the new config 
      $this->upload->initialize($config); 

      // Upload the second file 
      if ($this->upload->do_upload('userfile1')) 
      { 
       $data = $this->upload->data(); 
      } 
      else 
      { 
       echo $this->upload->display_errors(); 
      } 

     } 
    } 
    else 
    { 
     $this->load->view("upload_form"); 
    } 
} 
関連する問題