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

标签 codeigniter file-upload

Post_plot.php(这是我的 View 表单)

<form action="http://localhost:8080/ci/admin/plot/do_upload" enctype="multipart/form-data" method="post">

<input type="text" name="property_title" value=""  />

<input type="file" name="img" id="plot-img" value=""  />

<input type="submit" name="submit" value="Submit" />

</form> 

我有一个带有图片上传选项的长表单。

我正在使用 do_upload 函数上传文件

出现以下错误

Array ( [name] => a.jpg [type] => image/jpeg [tmp_name] => C:\xampp\tmp\phpBD75.tmp [error] => 0 [size] => 132277 ) Array ( [error] =>
You did not select a file to upload.

)

Plot.php Controller

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

class Plot extends CI_Controller {


    public function __construct()
    {
        parent::__construct();
        $this->load->library('Admin_layout');
        $this->load->model('admin/plot_model');
        $this->config->load('plot_rules');
        $this->output->enable_profiler(TRUE);
    }


    public function do_upload()
    {
            $config['upload_path']          = '../images/';
            $config['allowed_types']        = 'gif|jpg|png';
            $config['max_size']             = 100;
            $config['max_width']            = 1024;
            $config['max_height']           = 768;

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

            if ( ! $this->upload->do_upload($img))
            {
                    $error = array('error' => $this->upload->display_errors());

                    print_r($error);
            }
            else
            {
                    $data = array('upload_data' => $this->upload->data());

                    //$this->load->view('upload_success', $data);
                    print_r($data);
            }
    }//do_upload


}//class

我应该在 do_upload 函数中传递任何参数吗?

最佳答案

在这部分尝试把

$img = "img" // input name="img"
$this->upload->do_upload($img)

如果没有尝试/测试表单 action="http://localhost:8080/ci/admin/plot/do_upload"

Codeigniter 2 用户指南 http://www.codeigniter.com/userguide2/libraries/file_uploading.html

Codeigniter 3 用户指南 http://www.codeigniter.com/user_guide/libraries/file_uploading.html?highlight=file%20upload

您可能需要在 route.php 中设置一些路由

代码

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

class Plot extends CI_Controller {

public function __construct()
{
    parent::__construct();
    $this->load->library('Admin_layout');
    $this->load->model('admin/plot_model');
    $this->config->load('plot_rules');
    $this->output->enable_profiler(TRUE);
}

public function index() {
   $this->load->view('upload_form', array('error' => ' ' ));
}

public function do_upload()
{
        $config['upload_path'] = './images/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size']  = 100;
        $config['max_width']  = 1024;
        $config['max_height'] = 768;

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

        $img = "img";

        if ( ! $this->upload->do_upload($img))
        {
                $error = array('error' => $this->upload->display_errors());
                $this->load->view('upload_form', $error);

                print_r($error);
        }
        else
        {
                $data = array('upload_data' => $this->upload->data());

                $field_data = $this->upload->data();

                echo $field_data['file_name']; // etc 

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

关于codeigniter 3-您没有选择要上传的文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/29933119/

相关文章:

php - Codeigniter 强制所有流量发送至特定 Controller 和方法

php - 使用 PHP 和 cURL PUT 文件(内容)

javascript - 如何将 Fine Uploader js 框架与 ajax 结合使用

php - 如何确保上传的文件是MS Word文档?

javascript - 在 jquery ajax 中以不同方式上传数组中的文件

php - 使用 CodeIgniter 在数据库中记录更改的合适方法是什么

php - 通过codeigniter中的嵌套数组将多行插入数据库

node.js - 如何使用nodejs在mongodb内上传多个文件

php - CodeIgniter 子域路由最佳实践

php - Codeigniter 房间预订表格验证