Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/261.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/codeigniter/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php 在CodeIgniter中上载多个文件_Php_Codeigniter_File Upload - Fatal编程技术网

Php 在CodeIgniter中上载多个文件

Php 在CodeIgniter中上载多个文件,php,codeigniter,file-upload,Php,Codeigniter,File Upload,在我的CodeIgniter项目中,我在项目创建期间上载文件。以下是上传功能: function uploadFiles(){ $this->load->library('upload'); $error = 0; $projectName = $_POST['projectname']; mkdir('./uploads/'.$projectName); for($i=0; $i<count($_FILES);

在我的CodeIgniter项目中,我在项目创建期间上载文件。以下是上传功能:

function uploadFiles(){

     $this->load->library('upload');  
     $error = 0;    
     $projectName = $_POST['projectname'];
     mkdir('./uploads/'.$projectName);

     for($i=0; $i<count($_FILES); $i++)
     {

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

       $config['upload_path']   = './uploads/'.$projectName;
       $config['allowed_types'] = 'xml|etl';
       $config['max_size']      = '0';
       $config['overwrite']     = TRUE;

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

      if($this->upload->do_upload())
      {
        $error += 0;
      }else{
        $error += 1;
      }
     }

     if($error > 0){ return FALSE; }else{ return TRUE; }

}
然而,这没有任何作用。文件未上载。甚至没有创建空目录。有人能帮我找出错误吗


谢谢。

我想你错过了这个:

$config['file_name'] = 'somename_'.$i; $config['upload_path'] = './uploads/'.$projectName; ... $config['file_name']='somename.$i; $config['upload_path']='./uploads/'.$projectName; ...
这是CI_Upload类的扩展,我修改它来上载多个文件,只需将其复制到MY_Upload.php文件。它也会生成目录

然后,在控制器函数中需要做的就是将文件排列到数组中,其中字段名和键是数组键,配置是数据。在这种情况下:

public function create_project() {
    $this->load->library('form_validation');

    $this->form_validation->set_rules('projectname', 'Project name', 'required');

    $this->form_validation->set_message('required', 'This is an obligatory field');

    if($this->form_validation->run() == FALSE) {
        $this->load->view('project_creation_view');
    } else {
        $this->load->model('Project_management_model');
        $this->Project_management_model->create_project();
        $this->uploadFiles();
    }
}
$project_files['files'][0] = array(
            'upload_path' => './uploads/'.$projectName.'/',
            'max_size' => 0,
            'allowed_types' => 'xml|etl',
            'overwrite' => TRUE,
            'remove_spaces' => TRUE,
        );
$project_files['files'][1] = array(
            'upload_path' => './uploads/'.$projectName.'/',
            'max_size' => 0,
            'allowed_types' => 'xml|etl',
            'overwrite' => TRUE,
            'remove_spaces' => TRUE,
        );
如果所有配置的文件都相同,只需创建一个for循环来设置此设置,它还将采用“命名密钥”,即。
$project\u files['files']['file\u key']

然后打电话:

 if($this->upload->upload_files($project_files)){/*all files uploaded successfully*/}

我想我以前在这里看到过同样的问题;)


尝试输出$this->upload->display_errors(),其中增加$error变量。这会告诉你到底哪里出了问题。我的钱在权限上…

你可以上传任意数量的文件

$config['upload_path'] = 'upload/Main_category_product/';
$path=$config['upload_path'];
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '1024';
$config['max_width'] = '1920';
$config['max_height'] = '1280';
$this->load->library('upload', $config);

foreach ($_FILES as $fieldname => $fileObject)  //fieldname is the form field name
{
    if (!empty($fileObject['name']))
    {
        $this->upload->initialize($config);
        if (!$this->upload->do_upload($fieldname))
        {
            $errors = $this->upload->display_errors();
            flashMsg($errors);
        }
        else
        {
             // Code After Files Upload Success GOES HERE
        }
    }
}

我修改了Cubed Eye的MY_upload以返回所有上传文件的文件信息。实际上,在调用data方法时,只有最后一个文件的信息是可访问的。我还将上传程序的IP地址添加到该列表中

可通过以下方式使用:

$upload_files = $this->upload->upload_files($project_files);
if ($upload_files->status == true)
{
$fileInfo = $uploaded_files->file_info;
}

我意识到这将是更好的编辑立方眼,但这是我的第一篇文章,我没有声誉

为了简单起见,我创建了这个CI帮助文件。您可以从配置文件加载它,并在项目中的任何地方使用。它只需要3个参数

@fileobject=HTML文件对象的名称

@fileopath=要上载的文件路径

@filetypes=default*,ex-“jpg | png | mp4 | pdf”

function fileUploader($fileobject, $filepath = "uploads/", $filetypes = "*"){
// retrieve the number of images uploaded;
$number_of_files = sizeof($_FILES[$fileobject]['tmp_name']);

// considering that do_upload() accepts single files, we will have to do a small hack so that we can upload multiple files. For this we will have to keep the data of uploaded files in a variable, and redo the $_FILE.
$files = $_FILES[$fileobject];

$errors = array();
$file_arr = array();
$result = array();

// first make sure that there is no error in uploading the files
for ($k = 0; $k < $number_of_files; $k++) {
    if ($_FILES[$fileobject]['error'][$k] != 0) $errors[$k][] = 'Couldn\'t upload file ' . $_FILES[$fileobject]['name'][$k];
}
if (sizeof($errors) == 0) {
    // now, taking into account that there can be more than one file, for each file we will have to do the upload
    // we first load the upload library
    $CI =& get_instance();

    $CI->load->library('upload'); // load library

    // next we pass the upload path for the images
    $config['upload_path'] = FCPATH . $filepath;
    // also, we make sure we allow only certain type of images
    $config['allowed_types'] = $filetypes;
    //$config['max_size'] = (int)(ini_get('upload_max_filesize'));
    $config['max_size'] = '10000';
    $config['overwrite'] = FALSE;

    for ($i = 0; $i < $number_of_files; $i++) {
        $_FILES['uploadedimage']['name'] = $files['name'][$i];
        $_FILES['uploadedimage']['type'] = $files['type'][$i];
        $_FILES['uploadedimage']['tmp_name'] = $files['tmp_name'][$i];
        $_FILES['uploadedimage']['error'] = $files['error'][$i];
        $_FILES['uploadedimage']['size'] = $files['size'][$i];
        //now we initialize the upload library
        $CI->upload->initialize($config);
        // we retrieve the number of files that were uploaded
        if ($CI->upload->do_upload('uploadedimage')) {
            $imageup = $CI->upload->data();
           // print_r($imageup["orig_name"]);die;
            array_push($file_arr, $imageup["orig_name"]);
            $result["response"] = "true";
            $result["message"] = "Files uploaded successfully.";
            $result["files"] = $file_arr;

        } else {
            $result["response"] = "false";
            $result["message"][$i] = $CI->upload->display_errors();
        }
    }

} else {
    $result["response"] = "false";
    $result["message"] = $errors;

}
return $result;}
函数fileUploader($fileobject,$filepath=“uploads/”,$filetypes=“*”){
//检索上载的图像数量;
$number_of_files=sizeof($_files[$fileobject]['tmp_name']);
//考虑到do_upload()接受单个文件,我们将不得不做一个小的改动,以便上载多个文件。为此,我们必须将上载文件的数据保存在一个变量中,然后重做$_文件。
$files=$\u文件[$fileobject];
$errors=array();
$file_arr=array();
$result=array();
//首先确保上传文件时没有错误
对于($k=0;$k<$U文件的数量;$k++){
如果($\u文件[$fileobject]['error'][$k]!=0)$errors[$k][='无法上载文件。$\u文件[$fileobject]['name'][$k];
}
if(sizeof($errors)==0){
//现在,考虑到可能有多个文件,对于每个文件,我们都必须进行上传
//我们首先加载上载库
$CI=&get_instance();
$CI->load->library('upload');//加载库
//接下来,我们传递图像的上传路径
$config['upload_path']=FCPATH.$filepath;
//此外,我们确保只允许某些类型的图像
$config['allowed_types']=$filetypes;
//$config['max_size']=(int)(ini_get('upload_max_filesize'));
$config['max_size']='10000';
$config['overwrite']=FALSE;
对于($i=0;$i<$u文件数;$i++){
$\u文件['uploadeImage']['name']=$FILES['name'][$i];
$\u文件['uploadeImage']['type']=$FILES['type'][$i];
$\u FILES['uploadeImage']['tmp\u name']=$FILES['tmp\u name'][$i];
$\u文件['uploadeImage']['error']=$FILES['error'][$i];
$\u文件['uploadeImage']['size']=$FILES['size'][$i];
//现在我们初始化上传库
$CI->upload->initialize($config);
//我们检索上载的文件数
如果($CI->upload->do_upload('uploadedimage')){
$imageup=$CI->upload->data();
//打印($imageup[“原名]);模具;
数组推送($file\u arr,$imageup[“orig\u name”]);
$result[“response”]=“true”;
$result[“message”]=“文件已成功上载。”;
$result[“files”]=$file\u arr;
}否则{
$result[“response”]=“false”;
$result[“message”][$i]=$CI->upload->display_errors();
}
}
}否则{
$result[“response”]=“false”;
$result[“message”]=$errors;
}
返回$result;}

经过测试和验证。

由于我的声誉水平,我还不能发表评论,但Zoom在接受的答案下发表了评论,指出$fieldname变量是一个数组,导致了错误。我对这个答案也有同样的问题,并发现这是因为我在表单上的所有文件输入名都附加了[],这使得php将这些输入作为数组变量。要解决这个问题,只需给每个文件输入一个唯一的名称,而不是使其成为PHP数组。在我这么做之后,这个问题对我来说就消失了,被接受的答案就像一个符咒。我只是想把这些信息传给偶然发现这个问题的其他人。

是否有文件数的设置?我只能用codeigniter代码上传20个,与此没有太大区别。它给我的错误是\u uploaded\u file()期望参数1是字符串,数组给定,因为我尝试调试,$fieldname是数组。我尝试了你的代码,但这段代码有一个问题。如果用户上载文本文件或任何其他带有图像的非图像文件(文本文件+图像),则会上载其中一些图像,并返回文件类型错误。理想情况下,如果其中一个不是图像,则可能不允许使用任何单个图像。有什么帮助吗@srbhbarot@jason我最多也只能上传20个文件。想要一个解决方案。检查页面roytuts.com/codeigniter-multiple-files-upload/给每个文件输入一个唯一的名称是什么意思?使用单个
和使用多个输入f之间存在差异