首页 > 解决方案 > Codeigniter 上传文件为空

问题描述

我在上传文件时遇到问题。我正在使用 codeigniter 框架和 dropzone.js 来上传文件。

在我看来:

<!-- START MODAL ADD FILE -->
   <!-- Modal -->
    <div class="modal fade" id="myModalFile" tabindex="-1" role="dialog" aria-labelledby="myModalLabel">
      <div class="modal-dialog" role="document">
        <div class="modal-content">
          <div class="modal-header">
            <button type="button" class="close" data-dismiss="modal" aria-label="Close"><span aria-hidden="true">&times;</span></button>
            <h4 class="modal-title" id="myModalLabel">Nahrát Soubory</h4>
          </div>
          <div class="modal-body">           
                 <form action="<?php echo base_url("items/upload_agreement"); ?>" class="dropzone" method="POST" enctype="multipart/form-data">
                      <input type="hidden" name="agreement_dir_name" value="<?= $agreementDir;?>"  >
                      <input type="hidden" name="dir_name" value="<?= $customer_dir;?>"  >
                      <input type="hidden" name="customer_id" value="<?= $customerID;?>"  >
                      <input type="hidden" name="agreement_id" value="<?= $agreementID;?>"  > 
                    <div class="fallback">
                      <input name="fileToUpload[]" type="file" id="fileToUpload" multiple />
                    </div>               
          </div>
          <div class="modal-footer">
            <button type="button" class="btn btn-danger btn-default" data-dismiss="modal">Zavřít</button>
            <input type="submit" class="btn btn-success btn-default" value="Nahrát" >
            </form>
          </div>

        </div>
      </div>
    </div>
<!-- END MODAL ADD FILE -->

控制器:

public function upload_agreement()
{
$customerDir = $this->input->post('dir_name');
$agreementDir = $this->input->post('agreement_dir_name');

$config['upload_path']          = "/www/Cesarlease2/leases/$customerDir/$agreementDir/";


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

                $data = array('upload_data' => $this->upload->data());
                $this->session->set_flashdata('success_msg', 'Soubor úspěšně nahrán');
                redirect("items/items/".$this->input->post('customer_id')."/".$this->input->post('agreement_id'));

}

问题是上传的文件返回为空:

数组(1) { ["upload_data"]=> 数组(14) { ["file_name"]=> string(0) "" ["file_type"]=> string(0) "" ["file_path"]=> string(56) "/www/Cesarlease2/leases/testircrswqe789745asda/2-42017a/" ["full_path"]=> string(56) "/www/Cesarlease2/leases/testircrswqe789745asda/2-42017a/" ["raw_name"] => bool(false) ["orig_name"]=> string(0) "" ["client_name"]=> string(0) "" ["file_ext"]=> string(0) "" ["file_size"] => NULL ["is_image"]=> bool(false) ["image_width"]=> NULL ["image_height"]=> NULL ["image_type"]=> string(0) "" ["image_size_str"]=>字符串(0)“”}}

我尝试转储 fileToUpload 输入的 POST 值,也是空的。有什么我忽略的吗?

标签: phpcodeigniterdropzone

解决方案


使用 codeigniter 上传库时,默认情况下需要一个名为 Post 的变量 userfile和一个 miltipart 形式。

您可以使用此代码打开一个 miltipart 表单

<?php echo form_open_multipart('controller/method');?>

这可能会替换您的<form>标签,您可以使用此代码将其关闭

<?php echo form_close(); ?>

或者你可以继续使用你的</form>标签

在您的控制器中,由于您使用的是不同的后变量名称和数组,因此您应该编写一个类似的循环

$i=0
foreach ($this->input-post('fileToUpload') as $p ){
    $data[$i] = array('upload_data' =>$this->upload->data($p));
    $i++;
}

这可能有效,您也可能希望避免像配置路径中那样的完整路径,这将很难在部署时更改并且您可能会遇到很多错误,相反您可以使用类似的东西<?php base_url('anyFolderInYourProject/anySubfolder'); ?>给您完整的路径您的项目目录并添加您放入的任何路线。

希望这会有所帮助,问候。


推荐阅读