2013-02-17 206 views
0

我試圖構建一些應用程序,但在用ci編寫的代碼中遇到挫敗,我使用了multi_upload。 我上傳了一些文件並通過電子郵件發送,但CI file_path必須上傳到服務器根目錄,因此我嘗試上傳並找到file_path多文件上傳只是顯示上次上傳的文件

這裏是我的控制器:

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 

class Upload extends CI_Controller { 

function index() 
{  
    $this->load->view('upload_form'); 
} 

function do_upload() 
{ 

    $config['upload_path'] = './uploads/'; // server directory 
    $config['allowed_types'] = 'gif|jpg|png'; // by extension, will check for whether it is an image 
    $config['max_size'] = '1000'; // in kb 
    $config['max_width'] = '1024'; 
    $config['max_height'] = '768'; 

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

    $files = $this->multi_upload->go_upload(); 

    if ($files)   
    { 
     $error = array('error' => $this->upload->display_errors()); 
     $this->load->view('upload_form', $error); 
    }  
    else 
    { 

     $data = array(
       'upload_data' =>$this->upload->data() 


     ); 
     $this->load->view('upload_success', $data); 
    } 
}  
} 
?> 

upload_form

<?php if (isset($error)) echo $error;?> 
<form action="http://localhost/hemmmm/index.php/upload/do_upload" method="post" accept-charset="utf-8" enctype="multipart/form-data"> 
<input type="file" name="userfile[]" size="20" class="multi" 
    data-ajax="false"><br /><br /> 

    <input type="submit" value="upload" /> 

我succes_form:

<?php if (! defined('BASEPATH')) exit('No direct script access allowed'); ?> 
    <html> 
    <head> 
    <title>Upload Form</title> 
    </head> 
    <body> 

    <h3>Your file was successfully uploaded!</h3> 

    <ul> 

    <?php foreach ($upload_data as $item => $value):?> 
    <li><?php echo $item;?>: <?php echo $value;?></li> 
    <?php endforeach; ?> 
</ul> 

<p><?php echo anchor('upload', 'Upload More Files!'); ?></p> 

</body> 
</html> 

我multi_upload庫:

 <?php if (! defined('BASEPATH')) exit('No direct script access allowed'); 

/** 

    * This library assumes that you have already loaded the default CI Upload Library seperately 
    * 
    * Functions is based upon CI_Upload, Feel free to modify this 
    * library to function as an extension to CI_Upload 
    * 
    * Library modified by: Alvin Mites 
    * http://www.mitesdesign.com 
    * 
    */ 

class Multi_upload { 
    function Multi_upload() { 
//  $CI =& get_instance(); 
    } 

    /** 
    * Perform multiple file uploads 
    * Based upon JQuery Multiple Upload Class 
    * see http://www.fyneworks.com/jquery/multiple-file-upload/ 
    */ 
    function go_upload($field = 'userfile') { 
     $CI =& get_instance(); 
     // Is $_FILES[$field] set? If not, no reason to continue. 
     if (! isset($_FILES[$field]['name'][0])) 
     { 
      $CI->upload->set_error('upload_no_file_selected'); 
      return FALSE; 
     } else 
     { 
      $num_files = count($_FILES[$field]['name']) -1; 
      $file_list = array(); 
      $error_hold = array(); 
      $error_upload = FALSE; 
     } 

     // Is the upload path valid? 
     if (! $CI->upload->validate_upload_path()) 
     { 
      // errors will already be set by validate_upload_path() so just return FALSE 
      return FALSE; 
     } 

     for ($i=0; $i < $num_files; $i++) { 

//   $fname = $_FILES[$field]['name'][$i]; 
//   echo "$fname\n\n<br><br>\n\n"; 

      $error_hold[$i] = FALSE; 

      // Was the file able to be uploaded? If not, determine the reason why. 
      if (! is_uploaded_file($_FILES[$field]['tmp_name'][$i])) 
      { 
       $error = (! isset($_FILES[$field]['error'][$i])) ? 4 : $_FILES[$field]['error'][$i]; 

       switch($error) 
       { 
        case 1: // UPLOAD_ERR_INI_SIZE 
         $error_hold[$i] = 'upload_file_exceeds_limit'; 
         break; 
        case 2: // UPLOAD_ERR_FORM_SIZE 
         $error_hold[$i] = 'upload_file_exceeds_form_limit'; 
         break; 
        case 3: // UPLOAD_ERR_PARTIAL 
         $error_hold[$i] = 'upload_file_partial'; 
         break; 
        case 4: // UPLOAD_ERR_NO_FILE 
         $error_hold[$i] = 'upload_no_file_selected'; 
         break; 
        case 6: // UPLOAD_ERR_NO_TMP_DIR 
         $error_hold[$i] = 'upload_no_temp_directory'; 
         break; 
        case 7: // UPLOAD_ERR_CANT_WRITE 
         $error_hold[$i] = 'upload_unable_to_write_file'; 
         break; 
        case 8: // UPLOAD_ERR_EXTENSION 
         $error_hold[$i] = 'upload_stopped_by_extension'; 
         break; 
        default : 
         $error_hold[$i] = 'upload_no_file_selected'; 
         break; 
       } 

       return FALSE; 
      } 

      // Set the uploaded data as class variables 
      $CI->upload->file_temp = $_FILES[$field]['tmp_name'][$i];    
      $CI->upload->file_name = $this->_prep_filename($_FILES[$field]['name'][$i]); 
      $CI->upload->file_size = $_FILES[$field]['size'][$i];   
      $CI->upload->file_type = preg_replace("/^(.+?);.*$/", "\\1", $_FILES[$field]['type'][$i]); 
      $CI->upload->file_type = strtolower($CI->upload->file_type); 
      $CI->upload->file_ext = $CI->upload->get_extension($_FILES[$field]['name'][$i]); 

      // Convert the file size to kilobytes 
      if ($CI->upload->file_size > 0) 
      { 
       $CI->upload->file_size = round($CI->upload->file_size/1024, 2); 
      } 

      // Is the file type allowed to be uploaded? 
      if (! $CI->upload->is_allowed_filetype()) 
      { 
       $error_hold[$i] = 'upload_invalid_filetype'; 
      } 

      // Is the file size within the allowed maximum? 
      if (! $CI->upload->is_allowed_filesize()) 
      { 
       $error_hold[$i] = 'upload_invalid_filesize'; 
      } 

      // Are the image dimensions within the allowed size? 
      // Note: This can fail if the server has an open_basdir restriction. 
      if (! $CI->upload->is_allowed_dimensions()) 
      { 
       $error_hold[$i] = 'upload_invalid_dimensions'; 
      } 

      // Sanitize the file name for security 
      $CI->upload->file_name = $CI->upload->clean_file_name($CI->upload->file_name); 

      // Remove white spaces in the name 
      if ($CI->upload->remove_spaces == TRUE) 
      { 
       $CI->upload->file_name = preg_replace("/\s+/", "_", $CI->upload->file_name); 
      } 

      /* 
      * Validate the file name 
      * This function appends an number onto the end of 
      * the file if one with the same name already exists. 
      * If it returns false there was a problem. 
      */ 
      $CI->upload->orig_name = $CI->upload->file_name; 

      if ($CI->upload->overwrite == FALSE) 
      { 
       $CI->upload->file_name = $CI->upload->set_filename($CI->upload->upload_path, $CI->upload->file_name); 

       if ($CI->upload->file_name === FALSE) 
       { 
        $error_hold[$i] = TRUE; 
       } 
      } 

      /* 
      * Move the file to the final destination 
      * To deal with different server configurations 
      * we'll attempt to use copy() first. If that fails 
      * we'll use move_uploaded_file(). One of the two should 
      * reliably work in most environments 
      */ 
      if (! @copy($CI->upload->file_temp,  $CI->upload->upload_path.$CI->upload->file_name)) 
      { 
       if (! @move_uploaded_file($CI->upload->file_temp, $CI->upload->upload_path.$CI->upload->file_name)) 
       { 
        $error_hold[$i] = 'upload_destination_error'; 
       } 
      } 

      /* 
      * Run the file through the XSS hacking filter 
      * This helps prevent malicious code from being 
      * embedded within a file. Scripts can easily 
      * be disguised as images or other file types. 
      */ 
      if ($CI->upload->xss_clean == TRUE) 
      { 
       $CI->upload->do_xss_clean(); 
      } 

      if ($error_hold[$i]) { 
       $error_upload = TRUE; 

//    echo $error_hold[$i]; 
      } else { 
       if ($imageVar = $this->multiple_image_properties($CI->upload->upload_path.$CI->upload->file_name)) { 

        $file_list[] = array(
          'name' => $CI->upload->file_name, 
          'file' => $CI->upload->upload_path.$CI->upload->file_name, 
          'size' => $CI->upload->file_size, 
          'ext' => $CI->upload->file_ext, 
          'image_type' => $imageVar->image_type, 
          'height' => $imageVar->height, 
          'width' => $imageVar->width 
          ); 
       } else { 
        $file_list[] = array(
          'name' => $CI->upload->file_name, 
          'file' => $CI->upload->upload_path.$CI->upload->file_name, 
          'size' => $CI->upload->file_size, 
          'type' => $CI->upload->file_type, 
          'ext' => $CI->upload->file_ext, 
          ); 
       } 
      } 

// For debugging 
/*    
      if (strlen($error_hold[$i]) > 1) { 
        print_r($error_hold); 
      } 
*/    
     } // end for loop 

// Add error display for individual files   
     if ($error_upload) { 
      $this->set_error($error_hold); 
      return FALSE; 
     } else { 
      return $file_list; 
     }  
    } 

    // -------------------------------------------------------------------- 

    /** 
    * Set Image Properties 
    * 
    * Uses GD to determine the width/height/type of image 
    * 
    * @access public 
    * @param string 
    * @return void 
    */  
    function multiple_image_properties($path = '') 
    { 
     $CI =& get_instance(); 
     if (! $CI->upload->is_image()) 
     { 
      return false; 
     } 

     if (function_exists('getimagesize')) 
     { 
      if (FALSE !== ($D = @getimagesize($path))) 
      {  
       $types = array(1 => 'gif', 2 => 'jpeg', 3 => 'png'); 

       $image->width  = $D['0']; 
       $image->height  = $D['1']; 
       $image->image_type  = (! isset($types[$D['2']])) ? 'unknown' : $types[$D['2']]; 

       return $image; 
      } 
     } 
    } 

    // -------------------------------------------------------------------- 

    /** 
    * Set an error message 
    * 
    * @access public 
    * @param string 
    * @return void 
    */  
    function set_error($msg) 
    { 
     $CI =& get_instance();  
     $CI->lang->load('upload'); 

     if (is_array($msg)) 
     { 
      foreach ($msg as $val) 
      { 
       $msg = ($CI->lang->line($val) == FALSE) ? $val : $CI->lang->line($val);     
       $this->error_msg[] = $msg; 
       log_message('error', $msg); 
      }   
     } 
     else 
     { 
      $msg = ($CI->lang->line($msg) == FALSE) ? $msg : $CI->lang->line($msg); 
      $this->error_msg[] = $msg; 
      log_message('error', $msg); 
     } 
    } 

    // -------------------------------------------------------------------- 
protected function _prep_filename($filename) { 

     $CI = & get_instance(); 

     if (strpos($filename, '.') === FALSE OR $CI->upload->allowed_types == '*') { 
      return $filename; 
     } 

     $parts = explode('.', $filename); 
     $ext = array_pop($parts); 
     $filename = array_shift($parts); 

     foreach ($parts as $part) { 
      if (!in_array(strtolower($part), $CI->upload->allowed_types) OR $CI->upload->mimes_types(strtolower($part)) === FALSE) { 
       $filename .= '.' . $part . '_'; 
      } else { 
       $filename .= '.' . $part; 
      } 
     } 

     $filename .= '.' . $ext; 

     return $filename; 
    } 

    } 
?> 

當我嘗試上傳一個文件時,它的工作原理和我對upload_data 有很好的說明,但是當我嘗試上載多個文件時,它的工作原理是上傳文件,但僅顯示上傳最後一個文件的 狀態。

回答

0

$ file_list數組每次從它的外觀處理圖像時都會被覆蓋。嘗試使用$ i變量在填充時增加它。

$file_list[$i] = array 
+0

我不能這樣做,我不知道在哪裏/我必須鍵入什麼代碼,請geve我的代碼 – KEKUATAN 2013-02-17 15:47:25

+0

CMON先生裏克如果你知道somethink請分享您的knowladge,我得失眠的只是解決這個問題我self – KEKUATAN 2013-02-17 16:08:37

+0

它在多重上傳函數cmon Frog中填充數組,讀取自己的代碼。 – 2013-02-17 16:22:15