php - 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">×</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 值,也是空的。有什么我忽略的吗?
解决方案
使用 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'); ?>
给您完整的路径您的项目目录并添加您放入的任何路线。
希望这会有所帮助,问候。
推荐阅读
- java - 从 API 中不断读取 JSON 的最有效技术是什么?
- sharepoint - MS Flow 从 TXT 文件填充 SharePoint 列表
- javascript - Javascript 和 Twitter API
- r - 如何在同一列中拆分以逗号分隔的元素?
- javascript - Chrome 扩展:为所有网络工作者修改内置功能
- java - 如果值应该是默认值,如何强制开发人员使用不带参数的构造函数?
- python - 您正在被重定向 Javascript 是必需的 python requests_html
- javascript - 网站未检测到我在 SCSS 上创建的形状
- json - Integromat - 从 rpc 动态渲染集合的规范
- c++ - 一个大小为 5 的数组中可以插入多少个元素?