2015-11-18 167 views
3

我想上传文件夹中的多个文件,但它给出错误“您没有选择要上传的文件”。
甲PHP错误遇到CodeIgniter多文件上传不起作用

严重性:警告

消息:is_uploaded_file()预计参数1是字符串,给出

文件名阵列 :库/ upload.php的

行号:412

Backtrace:

文件:C:\瓦帕\ WWW \ mshaadi \应用\控制器\ Email.php线:55 功能:do_upload

文件:C:\瓦帕\ WWW \ mshaadi \ index.php的线:293功能: require_once

控制器

$conf['upload_path'] = './images'; 
    $conf['allowed_types'] = 'doc|docx|pdf|jpg|gif|jpeg|png'; 
    $conf['max_size'] = '9999000'; 
    $conf['max_width'] = '1024'; 
    $conf['max_height'] = '768'; 
    $conf['overwrite'] = TRUE; 
    $this->load->library('upload'); 
    foreach ($_FILES as $fieldname => $fileObject){ 
      $this->upload->initialize($conf); 
     if (!empty($fileObject['name'])){ 
      if (!$this->upload->do_upload($fieldname)){ 
       $error = $this->upload->display_errors(); 
       print_r($error); 
      }else{ 
       print_r("done"); 
      } 
     }else { 
      print_r("no"); 
     } 
    } 

视图

<div class="form-group col-md-12"> 
     <label for="Attach"><strong>Add Attachment</strong><br></label> 
     <input type="file" class="btn btn-default btn-file" name="atta[]" id="Attach" multiple="multiple"> 
</div> 
+0

在你** **控制器,我不能看到任何'is_uploaded_file()'函数? –

+0

不,我没有使用过这个功能..... –

+0

do_upload();是用 –

回答

2

尝试这样,

function upload_files() 
{  
    $config = array(); 
    $config['upload_path'] = './Images/'; 
    $config['allowed_types'] = 'gif|jpg|png'; 
    $config['max_size']  = '0'; 
    $config['overwrite']  = FALSE; 

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

    $files = $_FILES; 
    for($i=0; $i< count($_FILES['userfile']['name']); $i++) 
    {   
     $_FILES['userfile']['name']= $files['userfile']['name'][$i]; 
     $_FILES['userfile']['type']= $files['userfile']['type'][$i]; 
     $_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i]; 
     $_FILES['userfile']['error']= $files['userfile']['error'][$i]; 
     $_FILES['userfile']['size']= $files['userfile']['size'][$i];  

     $this->upload->initialize($config); 
     $this->upload->do_upload(); 
    } 
} 
+0

不,它不工作 –

+0

你是否得到任何错误? –

+0

用户文件被“atta”取代。 –

1

这是工作

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

     $files = $_FILES; 
     $cpt = count($_FILES['userfile']['name']); 
     for($i=0; $i<$cpt; $i++) 
     {   
      $_FILES['userfile']['name']= $files['userfile']['name'][$i]; 
      $_FILES['userfile']['type']= $files['userfile']['type'][$i]; 
      $_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i]; 
      $_FILES['userfile']['error']= $files['userfile']['error'][$i]; 
      $_FILES['userfile']['size']= $files['userfile']['size'][$i];  

      $this->upload->initialize($this->set_upload_options()); 
      $this->upload->do_upload(); 
     } 
    } 

    private function set_upload_options() 
    { 
     //upload an image options 
     $config = array(); 
     $config['upload_path'] = './Images/'; 
     $config['allowed_types'] = 'gif|jpg|png'; 
     $config['max_size']  = '0'; 
     $config['overwrite']  = FALSE; 

     return $config; 
    } 
0

我要我的答案作为一个更全面的解释添加@ SachinMarwa自己的答案。我提交的代码并不是不同的代码,而是添加了他的答案中未提及的一些行和特定内容。

即使看起来他的回答在技术上是正确的,也许他的回答也是正确的,但他们并不适合我。我必须研究这个问题以找出真正发生的事情,并且我了解了如何编写我自己的解决方案的过程中了解了足够多。

首先,请参阅Nana Partykar的评论,“在您的控制器中,我无法看到任何is_uploaded_file()函数?”该评论告诉我们,人们误解了两个名称相似但文件不同的文件。我知道,因为有一段时间我以为他们一定是指同一个文件,控制器文件(名为“Uploader.php”)。我几乎可以看到所有这些问题都提到了相同的“如何使用Ajax上传多个文件”教程,其中包括我自己的版本。我们都在使用的代码是完全一样的。

但是,控制器文件是“Uploader.php”。你看到$ this-> upload-> do_upload()或$ this-> upload-> do_upload('userfile')甚至$ this-> upload-> do_upload('files'),这是指系统/库模块文件名为“Upload.php”。请注意,在调用do_upload()函数之前,您必须调用以下行:$ this-> load-> library('upload',$ config);

Sachin Marwha给了我们一个循环遍历$ _FILES ['userfile']数组的循环。假设你上传了三张照片。每个$ _FILES ['userfile']元素本身由5个“属性”组成:name,type,tmp_name,error,size。您可以在PHP上看到这些$ _FILE属性。

你只是想传递一个文件在一次do_upload()。您不希望一次将所有三个(甚至20个)文件传递给do_upload。这意味着在调用do_upload()之前,必须将$ _FILES ['userfile']数组分解为单个文件。为此,我创建$ _FILES数组的$ _FILES ['f']元素。我通过在do_upload($ file ='userfile')函数中的system/library/Upload.php文件中设置断点来了解这一点,以了解我在哪里得到臭名昭着的“没有选择要上传的文件” (包括我自己)一直在抱怨。你会发现,这个函数使用你的表单发送给你的控制器的原始$ _FILES数组。但它实际上只使用表单中输入类型=文件的名称。如果您不告诉它表单输入的名称,它将默认为$ _FILES ['userfile']。事实证明,这是我最大的问题,因为如果我使用输入字段的名称,那么该字段会传递一个数组或一组文件,而不仅仅是一个文件。所以我不得不做一个特殊的$ _FILES ['f]元素,并且只传递$ _FILES ['f']。

这里是我做的方式,相信我,我尝试了所有版本的这个页面和其他人,而不仅仅是一个StackOverflow的,但其他的教程上,还有:

$cpt = count($_FILES['userfile']['name']); 
    for($i=0; $i < $cpt; $i++) 
    { 
     unset($config); 
     $config = array(); 
     $config['upload_path'] = $path; 
     $config['allowed_types'] = 'gif|jpg|png'; 
     $config['max_size'] = '1000'; 
     $config['overwrite'] = TRUE; 
     $config['remove_spaces'] = FALSE; 
     $config['file_name'] = $_FILES['userfile']['name'][$i]; 

     // Create a new 'f' element of the $_FILES object, and assign the name, type, tmp_name, error, and size properties to the corresponding 'userfile' of this iteration of the FOR loop. 
     $_FILES['f']['name'] = $_FILES['userfile']['name'][$i]; 
     $_FILES['f']['type'] = $_FILES['userfile']['type'][$i]; 
     $_FILES['f']['tmp_name'] = $_FILES['userfile']['tmp_name'][$i]; 
     $_FILES['f']['error'] = $_FILES['userfile']['error'][$i]; 
     $_FILES['f']['size'] = $_FILES['userfile']['size'][$i]; 

     $this->load->library('upload', $config); 
     $this->upload->initialize($config);    
     if (! $this->upload->do_upload('f')) 
     { 
      $data['errors'] = $this->upload->display_errors(); 
     } 
     else 
     { 
      $data['errors'] = "SUCCESS"; 
     } 

     unset($config); 
     $config = array(); 
     $config['image_library'] = 'gd2'; 
     $config['source_image']  = $path . $_FILES['userfile']['name'][$i]; 
     $config['create_thumb']  = TRUE; 
     $config['maintain_ratio'] = TRUE; 
     $config['thumb_marker']  = '.thumb'; 
     $config['width']   = 100; 
     $config['height']   = 100; 

     $this->load->library('image_lib', $config); 
     $this->image_lib->clear(); 
     $this->image_lib->initialize($config); 
     $this->image_lib->resize(); 
     $types = array('.jpg'); 
    }   

凡会取消for循环中的$ config数组,然后重新映射$ config数组,这就是为每个图片文件制作缩略图的部分。

完整控制器上载功能:

public function upload_asset_photo() 
    { 
     $data = array(); 
     $dateArray = explode("/",$this->input->post('date')); 
     $date = $dateArray[2] . "/" . $dateArray[0] . "/" . $dateArray[1]; // year/month/day 
     $cid = $this->config->item('cid'); // this is a special company id I use, unnecessary to you guys. 
     $padded_as_id = sprintf("%010d", $this->uri->segment(3)); // this makes an "asset id" like "3" into "0000000003" 
     $path = 'properties_/' . $padded_as_id . '/' . $date . '/'; // file path 
     if (!is_dir($path)) { 
      mkdir($path,0755,true); //makes the ile path, if it doesn't exist 
     } 

     $cpt = count($_FILES['userfile']['name']); 
     for($i=0; $i < $cpt; $i++) 
     { 
      unset($config); 
      $config = array(); 
      $config['upload_path'] = $path; 
      $config['allowed_types'] = 'gif|jpg|png'; 
      $config['max_size'] = '1000'; 
      $config['overwrite'] = TRUE; 
      $config['remove_spaces'] = FALSE; 
      $config['file_name'] = $_FILES['userfile']['name'][$i]; 

      $_FILES['f']['name'] = $_FILES['userfile']['name'][$i]; 
      $_FILES['f']['type'] = $_FILES['userfile']['type'][$i]; 
      $_FILES['f']['tmp_name'] = $_FILES['userfile']['tmp_name'][$i]; 
      $_FILES['f']['error'] = $_FILES['userfile']['error'][$i]; 
      $_FILES['f']['size'] = $_FILES['userfile']['size'][$i]; 

      $this->load->library('upload', $config); 
      $this->upload->initialize($config);    
      if (! $this->upload->do_upload('f')) 
      { 
       $data['errors'] = $this->upload->display_errors(); 
      } 
      else 
      { 
       $data['errors'] = "SUCCESS"; 
      } 

      unset($config); 
      $config = array(); 
      $config['image_library'] = 'gd2'; 
      $config['source_image']  = $path . $_FILES['userfile']['name'][$i]; 
      $config['create_thumb']  = TRUE; 
      $config['maintain_ratio'] = TRUE; 
      $config['thumb_marker']  = '.thumb'; 
      $config['width']   = 100; 
      $config['height']   = 100; 

      $this->load->library('image_lib', $config); 
      $this->image_lib->clear(); 
      $this->image_lib->initialize($config); 
      $this->image_lib->resize(); 
      $types = array('.jpg'); 
     }   

     header('Content-Type: application/json'); 
     echo json_encode($data); 
    }