您错过了在do_upload函数中添加文件输入的name属性。
我遇到了类似"您没有选择要上传的文件"的错误。这是一个奇怪的问题,我不知道我的代码哪里出错了。
我的控制器代码如下:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Dashboard extends MY_Controller {
public function do_upload()
{
ini_set("display_errors",1); // I added so it would help me show errors
error_reporting(E_ALL);
//print_r($this->input->post());exit(); // post result shows "submit = submit"
$config['upload_path'] = '/assets/uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 10000;
//$config['max_width'] = 1024;
//$config['max_height'] = 768;
//print_r($config);exit();
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('login/header');
$this->load->view('login/dashboard/add_logo', $error);
$this->load->view('login/footer');
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
}
我只是想在我的页面上传一个标志。我没有存储到数据库中,所以现在没有模型。我的视图代码如下:
<div class="content">
<div class="container-fluid">
<div class="row">
<div class="col-md-12">
<div class="card">
<div class="col-md-12">
<div class="header">
<h4 class="title">Header/Footer Logo</h4>
</div>
</div>
<div class="col-md-12">
<form action="<?php echo site_url();?>/dashboard/do_upload" role="form" method="post" enctype="multipart/form-data">
<div class="panel">
<div class="panel-body">
<b><?php if(isset($error)) echo $error; ?></b>
<div class="form-group">
<label>Logo</label>
<input class="form-control" type="file" name="picture" />
</div>
<div class="form-group">
<input type="submit" class="btn btn-warning" name="submit" value="submit">
</div>
</div>
</div>
</form>
</div>
</div>
</div>
</div>
</div>
</div>
您有:<input class="form-control" type="file" name="picture" />
所以就这么做吧:
if ( ! $this->upload->do_upload('picture')){
// your code
}