我有一个codeigniter项目,我也试图上传文件。当我提交表单时,我得到了一个
的数据数组 Array ( [file_name] => [file_type] => [file_path] => ./uploads/ [full_path] => ./uploads/ [raw_name] => [orig_name] => [client_name] => [file_ext] => [file_size] => [is_image] => [image_width] => [image_height] => [image_type] => [image_size_str] => )
即使我将文件放在输入字段中。
的index.php
<?php $this->load->view('include/document_header'); ?>
<script src="<?php echo base_url(); ?>assets/script/fancybox/jquery.fancybox.pack.js"></script>
<link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>assets/script/fancybox/jquery.fancybox.css" />
<script src="<?php echo base_url(); ?>assets/script/jquery.filters.js"></script>
<link rel="stylesheet" type="text/css" href="<?php echo base_url(); ?>assets/style/jquery.filters.css" />
<script src="<?php echo base_url(); ?>assets/script/jquery.json-2.4.js"></script>
<script>
var FILTER_NAMES = <?php echo json_encode($this->filter_model->get_filter_names()); ?>;
</script>
<script src="<?php echo base_url(); ?>assets/script/export.js"></script>
<?php $this->load->view('include/site_header'); ?>
<div id="content" class="gradient gray">
<h1 class="header">Import Contacts</h3>
<form id="import_form" action="<?php echo $import_url; ?>" method="post" target="_blank">
<h2 class="header">Copy CSV</h2>
<fieldset id="fields">
<div class="clearfix">
<label class="field">
<textarea name="copycsv" rows="4" cols="50"></textarea>
</label>
</div>
</fieldset>
<h2 class="header">Upload CSV</h3>
<fieldset id="fields">
<div class="clearfix">
<label class="field">
<input type="file" name="userfile" size="20" />
</label>
</div>
</fieldset>
<fieldset class="buttons clearfix">
<input type="submit" name="submit" value="Submit" class="button gradient new_blue" />
</fieldset>
</form>
<div class="helper">
Download template CSV file. <a href="">Click here</a>
</div>
</div>
<?php $this->load->view('include/site_footer'); ?>
import.php控制器
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');
require FCPATH.'vendor/autoload.php';
use Goodby\CSV\Import\Standard\Lexer;
use Goodby\CSV\Import\Standard\Interpreter;
use Goodby\CSV\Import\Standard\LexerConfig;
class Import extends MY_Controller
{
function __construct()
{
// Call the parent constructor
parent::__construct();
if(!has_tab_access('import'))
{
redirect_tabs('import');
}
}
public function index()
{
$data = array(
'import_url' => site_url(array('import', 'process'))
);
// Load the form view
$this->load->view('import/index', $data);
}
public function process()
{
$fields = $this->input->post();
var_dump($this->input->post());
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'text/comma-separated-values|application/csv|application/excel|application/vnd.ms-excel|application/vnd.msexcel|text/anytext';
$config['max_size'] = '1000';
$this->load->library('upload');
$this->upload->initialize($config);
if (!$this->upload->do_upload('userfile')){
echo $this->upload->display_errors();
print_r($this->upload->data());
}else{
}
#check to make sure fields are entered. If none of them have content
#error message displayed
/*if(strlen($fields['copycsv']) == 0 || strlen($fields['userfile']) == 0){
#$this->load->view('import/index', $data);
$this->load->library('user_agent');
redirect($this->agent->referrer(), 'refresh');
}else{
if(strlen($fields['copycsv']) > 0 && strlen($fields['userfile']) == 0){
}else if(strlen($fields['userfile']) > 0 && strlen($fields['copycsv']) == 0){
}else{
}
}*/
}
}
/* End of file import.php */
/* Location: ./application/controllers/import.php */
不确定发生了什么。
答案 0 :(得分:1)
尝试使用form open multipart。表单上传需要表单类型为多部分。最好使用表单助手:
<?php echo form_open_multipart('controller/method');?>
在您的情况下复制您的信息:
<?php echo form_open_multipart($import_url, array('id'=>'import_form', 'target'=>'_blank'));?>
您可以在此处的文档中看到: https://www.codeigniter.com/user_guide/libraries/file_uploading.html#creating-the-upload-form