codeigniter文件上传不上传文件

时间:2015-01-27 13:05:39

标签: php codeigniter file-upload

我正在尝试在codeigniter中上传文件,但它没有上传,即使是或者正在执行,也没有返回任何错误,只给它输出

string(9)“image / png”

这是我的代码 控制器:

$this->load->library('upload');
        if($_FILES['categoryIcon']['tmp_name'])
            {
                $ext=end(explode(".",$_FILES['categoryIcon']['name']));
                $filename=date('Ymdhis').rand().".".$ext;
                $config['upload_path']=dirname($_SERVER["SCRIPT_FILENAME"])."/assets/images/categories/";
                $config['upload_url']=base_url()."assets/images/categories/";
                $config['file_name']=$filename;
                $config['max_width']    = '2048';
                $config['max_height']   = '2048';
                $config['allowed_types'] = '*';
                $this->upload->initialize($config);
                if($this->upload->do_upload('categoryIcon'))
                {
                    $data['categoryIcon']=$filename;
                    //$response=$this->ecommerce_model->add_new_category($data);
                    echo "uploaded";
                }
                else{
                    echo $this->upload->display_errors();
                    echo "not uploaded";
                }
            }

查看页面

<form action="" method="post" enctype="multipart/form-data" class="practice_form"> <input type="file" name="categoryIcon" id="image" class="form-control" accept="image/*"><input type="submit" value="Save Category" class="btn btn-primary"/>

1 个答案:

答案 0 :(得分:0)

我有一个上传文件,你可以做的是<input type="file=" name="categoryIcon[]" multiple="multiple"/>并且应该拿起它你可以改变do_upload你想要的东西。

同时更改

$config['upload_path'] = dirname($_SERVER["SCRIPT_FILENAME"])."/assets/images/categories/";

至此$config['upload_path'] = "./assets/images/categories/";如果您在主目录中的上传文件夹不需要dirname,请参阅用户指南http://www.codeigniter.com/user_guide/libraries/file_uploading.html

回送文件数据

'$ file_info = $ this-&gt; upload-&gt; data();'

'echo $ file_info ['file_name'];'

只是一个示例适用于多个codeigniter上传。

<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Upload extends CI_Controller {

function index()
{

    $this->get_list();
}

public function get_list() { 
    $this->load->view('upload_success');
}

function do_upload() {

    $this->load->library('form_validation');

     // You need to set a form set rules 
    //$this->form_validation->set_rules('name', 'Banner Name', 'required');

    if ($this->form_validation->run()) {

        $files = $_FILES;

        $file_loop = count($_FILES['categoryIcon']['name']);       

        for($i= 0; $i < $file_loop; $i++) {

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

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

            if (!$this->upload->do_upload()) {          

                $this->get_form();

            } else {                

                $filename = $files['categoryIcon']['name'][$i];

                $this->get_list();

        }

    } else {

        $this->get_form();
    }
}

public function get_form() {
    $data['error'] = $this->upload->display_errors('<div class="alert alert-info">', '</div>');

    $this->load->view('upload_form', $data);
}

private function file_config() { 
    $config = array();
    $config['upload_path'] = './upload/';
    $config['allowed_types'] = 'gif|jpg|png';
    $config['max_size']      = '0';
    $config['overwrite']     = TRUE;

    return $config;
}
}