0

一切都很好,但是当我打开上传文件夹时,没有任何图像。

这是控制器“admin_news”。我尝试在 localhost/site/asset/upload 中上传图像文件。但是当我点击提交时,只有信息(标题、新闻和类别)进入数据库,但文件没有上传。

    function __construct()
{
    parent::__construct();
    $this->load->helper(array('form', 'html', 'file'));
    $config['upload_path']   = base_url('asset/upload/');
    $config['allowed_types'] = 'gif|jpg|png';
    $this->load->library('upload', $config);
}


public function add_news(){
    $this->load->helper('form');  
    $this->load->model('AddNews');  
    $this->load->view('templates/header', $data);
    $this->load->view('admin/add_news');
    $this->load->view('templates/footer');
    if($this->input->post('submit')){
        if ($this->upload->do_upload('image'))
        {
            $this->upload->initialize($config);
            $this->upload->data();
        }
        $this->AddNews->entry_insert();
        redirect("admin_news/index");
    }
}

该视图只有:

        <?php echo form_open_multipart(''); ?>
    <input type="text" name="title" value="Title..." onfocus="this.value=''" /><br /> 
    <input type="file" name="image" /><br />

……

4

2 回答 2

1

这不应该是 URL:

$config['upload_path']   = base_url('asset/upload/');

它应该是您服务器上某处的路径,可以是完整的绝对路径,也可以是相对路径(Codeigniter 中的所有路径都是相对于 index.php)。

使用其中之一:

// Full path
$config['upload_path'] = FCPATH.'asset/upload/';

// Relative
$config['upload_path'] = './asset/upload/';

还有一件事:

if ($this->upload->do_upload('image'))
{
    // You don't need this, and besides $config is undefined
    // $this->upload->initialize($config);

    // You don't seem to be doing anything with this?
    $this->upload->data();

    // Move this here in case upload fails
    $this->AddNews->entry_insert();
    redirect("admin_news/index");
}
// Make sure to show errors
else
{
    echo $this->upload->display_errors();
}
于 2012-12-29T19:21:00.317 回答
0

我认为在您的代码中,问题是您在 do_upload 方法之后声明了配置

if ($this->upload->do_upload('image'))
        {
            $this->upload->initialize($config);
            $this->upload->data();
        }

在使用该方法之前应该初始化配置。我认为这就是问题所在。do_upload所以你必须在方法之前进行配置

于 2012-12-30T07:00:15.550 回答