"上传路径似乎无效".Codeigniter文件上传无效

Ani*_*ket 20 codeigniter file-upload

我已经被困在这一点很长一段时间,甚至在阅读了一些帖子之后,我还没有找到解决方案.

我正在为测验创建一个界面,在管理界面中,我需要将图像上传到文件夹并将图像名称上传到数据库.我可以处理其他事情,但图像上传很长时间都困扰着我.

请看下面的代码.

表格

<?php echo form_open_multipart('admin/update_question'); ?>
<?php echo form_hidden('questionid', $question->level); ?>
<?php echo form_label('Comment', 'comment'); ?>
<div class="input">
<?php
    $arr_comment = array(
        'name'  => 'comment',
        'id'    => 'comment',
        'value' => $question->comment
        );
        echo form_textarea($arr_comment);
?>
</div>
<br/>
<?php echo form_label('Answer', 'answer'); ?>
<div class="input">
<?php
    $arr_answer = array(
            'name'  => 'answer',
            'id'    => 'answer',
            'size'  => '10000',
            'value' => $question->answer
    );
    echo form_input($arr_answer);
?>
</div>
<br/>
<?php echo form_label('Image', 'userfile'); ?>
<div class="input">
<?php
    $arr_image = array(
        'name'  => 'userfile',
        'id'  => 'userfile',
        'value' => ''
    );
    echo form_upload($arr_image);
?>
</div>
<br/>
<?php
$arr_button = array(
    'name'  => 'submit',
    'value' => 'Update Question',
    'class' => 'btn primary large'
    );
?>
<div class="input">
<?php echo form_submit($arr_button); ?>
</div>
<br/>
<?php
echo form_close();
if ($error != '')
    echo '<div class="alert-message error">'. $error.' </div>';
echo validation_errors();
?>
Run Code Online (Sandbox Code Playgroud)

我试过运行一个js,它返回上传框中的文件名,它确实返回了它.

控制器

public function update_question() {
        $comment = $this->input->post('comment');
        $answer = $this->input->post('answer');

        echo var_dump(is_dir(base_url().'uploads/'));

        /*
         * Uploading image
         */
        $config['upload_path'] = base_url().'/uploads/';
        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_width'] = 0;
        $config['max_height'] = 0;
        $config['max_size'] = 0;
        $config['encrypt_name'] = TRUE;

        $this->load->library('upload', $config);
        if ( ! $this->upload->do_upload())
        {
                $error = array('error' => $this->upload->display_errors());
                print_r($error);
        }
        else
        {
                $arr_image = array('upload_data' => $this->upload->data());
                print_r($arr_image);
        }

        $questionid = $this->input->post('questionid');
        $question_data = array(
            'comment'   => $comment,
            'answer'    => $answer
        );
        $this->load->model('adminmodel', 'admin');
        $question_data = $this->admin->update_question_data($questionid, $question_data);
        //redirect('admin/questions', 'location');
}
Run Code Online (Sandbox Code Playgroud)

对于我的上传路径,我尝试了一个或多个组合,并且唯一一个返回TRUE值的是var_dump(is_dir('/wamp/www/quark_edorado/uploads'));但是这也返回了相同的错误.

我不知道我哪里错了.

更新

我的目录结构是

/application/
/public/
    css/
    fonts/
    images/
    js/
/system/
/uploads/
Run Code Online (Sandbox Code Playgroud)

我正在操作Windows机器并使用WAMP.这有什么区别吗?

Ani*_*ket 50

我正在自动加载库,不知何故,当我尝试初始化配置时,$this->load->library('upload', $config);它不会这样做.

相反,我把我的配置文件 config/upload.php

另一种方法就是这样做 $this->upload->initialize($config);


Dau*_*Dau 9

好的,将你的上传文件夹放在你问题中显示的原始位置并尝试这个(给你base_url的相对路径)

$config['upload_path'] = 'uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_width'] = 0;
$config['max_height'] = 0;
$config['max_size'] = 0;
$config['encrypt_name'] = TRUE;

$this->load->library('upload', $config);
if ( ! $this->upload->do_upload()) {
    $error = array('error' => $this->upload->display_errors());
    print_r($error);
} else {
    $arr_image = array('upload_data' => $this->upload->data());
    print_r($arr_image);
}
Run Code Online (Sandbox Code Playgroud)