2013-01-23 124 views
1

从我下面的代码我想上传4张图片,但我只能够上传最后一张图片到我的upload文件夹。无法上传多个图像到其上传文件夹

我的图像验证和所有其他任务工作正常。但只有我有多个图像上传到其区别文件夹的问题。

请检查我的下面的代码,并帮助解决我的问题。

MyController.php

class Booksetups extends CI_Controller { 
    public function __construct() 
    { 
     parent:: __construct(); 
     $this->load->helper('url'); 
     $this->load->helper('form'); 
     $this->load->library('session'); 
     $this->load->model('Booksmodel'); 
     $this->load->library('form_validation'); 
     $this->load->library('pagination'); 
    } 

    public function valid_upload() 
    { 
     $this->load->library('upload'); 

     $config['upload_path'] = 'uploads/'; 
     $config['allowed_types'] = 'gif|jpg|jpeg|png'; 
     $config['max_size']   = 2048; 
     $config['max_width']  = 385; 
     $config['max_height']  = 410; 

     $this->upload->initialize($config); 

     if (!$this->upload->validate_upload('img1') || !$this->upload->validate_upload('img2') || !$this->upload->validate_upload('img3') || !$this->upload->validate_upload('img4')) { 
      $this->form_validation->set_message('valid_upload', $this->upload->display_errors()); 
      return FALSE; 
     } else { 
      return TRUE; 
     } 
    } 


    function book($book_id = 0) 
    { 
     $config = array(); 
     $config['base_url'] = 'http://localhost/thebestbookfinder.com/Booksetups/book/pgn/'; 
     $config["total_rows"] = $this->Booksmodel->record_count(); 
     $config['uri_segment'] = 4; 
     $config['per_page'] = 17; 
     $config['full_tag_open'] = '<div id="pagination">'; 
     $config['full_tag_close'] = '</div>'; 


     $this->pagination->initialize($config); 
     $page = ($this->uri->segment(4)) ? $this->uri->segment(4) : 0; 


     $this->form_validation->set_error_delimiters('<div class="error">', '</div>')->set_rules('book_title', '"Title"','trim|required|min_length[4]|max_length[150]|xss_clean'); 
     $this->form_validation->set_error_delimiters('<div class="error">', '</div>')->set_rules('edition_id', '"Edition Name"','trim|min_length[1]|max_length[150]|xss_clean'); 



     //---------------------validating images--------------------------- 

     $this->form_validation->set_rules('img1', 'Image 1', 'trim|xss_clean|callback_valid_upload'); 
     $this->form_validation->set_rules('img2', 'Image 2', 'trim|xss_clean|callback_valid_upload'); 
     $this->form_validation->set_rules('img3', 'Image 3', 'trim|xss_clean|callback_valid_upload'); 
     $this->form_validation->set_rules('img4', 'Image 4', 'trim|xss_clean|callback_valid_upload'); 



     //-----------Here i am uploading my images into my specified folder------------------------------- 
     if ($this->form_validation->run() === TRUE) { 
      $this->upload->do_upload('img1'); 
      $this->upload->do_upload('img2'); 
      $this->upload->do_upload('img3'); 
      $this->upload->do_upload('img4'); 
      $this->Booksmodel->entry_insert(); 
      $this->session->set_flashdata('msg', '1 row(s) inserted.'); 
      redirect(current_url()); 
     } 
     ................ 
     ................ 

My_Upload.php

Class My_Upload extends CI_Upload 
{ 

    public function __construct(){ 
     parent::__construct(); 
    } 

    public function validate_upload($field = 'img1') 
    { 

     // Is $_FILES[$field] set? If not, no reason to continue. 
     if (! isset($_FILES[$field])) { 
      $this->set_error('upload_no_file_selected'); 
      return FALSE; 
     } 

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

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

      switch ($error) { 
      case 1: 
       // UPLOAD_ERR_INI_SIZE 
       $this->set_error('upload_file_exceeds_limit'); 
       break; 
      case 2: 
       // UPLOAD_ERR_FORM_SIZE 
       $this->set_error('upload_file_exceeds_form_limit'); 
       break; 
      case 3: 
       // UPLOAD_ERR_PARTIAL 
       $this->set_error('upload_file_partial'); 
       break; 
      case 4: 
       // UPLOAD_ERR_NO_FILE 
       $this->set_error('upload_no_file_selected'); 
       break; 
      case 6: 
       // UPLOAD_ERR_NO_TMP_DIR 
       $this->set_error('upload_no_temp_directory'); 
       break; 
      case 7: 
       // UPLOAD_ERR_CANT_WRITE 
       $this->set_error('upload_unable_to_write_file'); 
       break; 
      case 8: 
       // UPLOAD_ERR_EXTENSION 
       $this->set_error('upload_stopped_by_extension'); 
       break; 
       default : $this->set_error('upload_no_file_selected'); 
       break; 
      } 

      return FALSE; 
     } 


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

     // Is the file type allowed to be uploaded? 
     if (! $this->is_allowed_filetype()) { 
      $this->set_error('upload_invalid_filetype'); 
      return FALSE; 
     } 

     // if we're overriding, let's now make sure the new name and type is allowed 
     if ($this->_file_name_override != '') { 
      $this->file_name = $this->_prep_filename($this->_file_name_override); 

      // If no extension was provided in the file_name config item, use the uploaded one 
      if (strpos($this->_file_name_override, '.') === FALSE) { 
       $this->file_name .= $this->file_ext; 
      } 

      // An extension was provided, lets have it! 
      else 
      { 
       $this->file_ext = $this->get_extension($this->_file_name_override); 
      } 

      if (! $this->is_allowed_filetype(TRUE)) { 
       $this->set_error('upload_invalid_filetype'); 
       return FALSE; 
      } 
     } 

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

     // Is the file size within the allowed maximum? 
     if (! $this->is_allowed_filesize()) { 
      $this->set_error('upload_invalid_filesize'); 
      return FALSE; 
     } 

     // Are the image dimensions within the allowed size? 
     // Note: This can fail if the server has an open_basdir restriction. 
     if (! $this->is_allowed_dimensions()) { 
      $this->set_error('upload_invalid_dimensions'); 
      return FALSE; 
     } 

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

     // Truncate the file name if it's too long 
     if ($this->max_filename > 0) { 
      $this->file_name = $this->limit_filename_length($this->file_name, $this->max_filename); 
     } 

     // Remove white spaces in the name 
     if ($this->remove_spaces == TRUE) { 
      $this->file_name = preg_replace("/\s+/", "_", $this->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. 
*/ 
     $this->orig_name = $this->file_name; 

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

      if ($this->file_name === FALSE) { 
       return FALSE; 
      } 
     } 

     /* 
* 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 ($this->xss_clean) { 
      if ($this->do_xss_clean() === FALSE) { 
       $this->set_error('upload_unable_to_write_file'); 
       return FALSE; 
      } 
     } 
     $this->set_image_properties($this->upload_path.$this->file_name); 
     return TRUE; 
    } 

    public function do_upload($field = 'img1') 
    { 
     /* 
* 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($this->file_temp, $this->upload_path.$this->file_name)) { 
      if (! @move_uploaded_file($this->file_temp, $this->upload_path.$this->file_name)) { 
       $this->set_error('upload_destination_error'); 
       return FALSE; 
      } 
     } 

     /* 
* Set the finalized image dimensions 
* This sets the image width/height (assuming the 
* file was an image). We use this information 
* in the "data" function. 
*/ 
     return TRUE; 
    } 
} 

MyViewForm.php

<?php echo form_open_multipart('Booksetups/book');?> 

<?php echo form_hidden('book_id',$fbook_id['value']);?> 

<input type="file" name="img1" /> 
<input type="file" name="img2" /> 
<input type="file" name="img3" /> 
<input type="file" name="img4" /> 
<?php 
    echo form_submit($submitbtn); 
    echo form_reset($resetbtn); 
?> 

<?php echo form_close();?>     
+0

也许我是一个老发* T,但我就是喜欢倍的图片上传成本核算时,只有约2几分钟的编程和最多20行代码。 –

+0

@MihaiIorga你是什么意思? –

+0

:)我只是在评论仇恨“新一代”上传脚本。 :) –

回答

1

使用jQuery的uploadify plugin.Its易于使用,并具有实时进度完全定制酒吧和更多的功能。

Uploadify demo

0

调试do_upload功能,在不使用局部变量$场的任何位置