douqiao2008 2017-01-26 16:51
浏览 61

codeigniter您没有选择要上传的文件

I have a codeigniter project that I am trying to upload a file too. When I submit the form I get back a data array of

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] => )

Even when I put a file in the input field.

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 controller

<?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 */

Not really sure what is going on.

  • 写回答

1条回答 默认 最新

  • du13520157325 2017-01-26 19:10
    关注

    Try using form open multipart. Form upload needs the form type to be multipart. It is best to do this with the form helper:

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

    In your case copying your info something like:

    <?php echo form_open_multipart($import_url, array('id'=>'import_form', 'target'=>'_blank'));?>
    

    Which you can see in the docs here: https://www.codeigniter.com/user_guide/libraries/file_uploading.html#creating-the-upload-form

    评论

报告相同问题?

悬赏问题

  • ¥15 对于知识的学以致用的解释
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败