“The upload path does not appear to be valid”. Cod

2019-02-04 08:06发布

I have been stuck at this point for a long time and even after reading a number of posts, I haven't been able to find a solution.

I am making an interface for a quiz, and in the admin interface, I need to upload images to a folder and upload the image name to the database. I can handle to other things but the image upload is bugging me alot for a long time.

Please take a look at my code below.

The Form

<?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();
?>

I have tried running a js which returns the filename in the upload box, and it does return it.

The Controller

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');
}

For my upload path, I have tried a number or combinations and the only one which returned a TRUE value was var_dump(is_dir('/wamp/www/quark_edorado/uploads')); but this also returned the same error.

I do not know where I am going wrong.

Update

My directory structure is

/application/
/public/
    css/
    fonts/
    images/
    js/
/system/
/uploads/

I am operating a Windows machine and using WAMP. Does that make a difference?

3条回答
该账号已被封号
2楼-- · 2019-02-04 08:17

I was autoloading the library and somehow when I was trying to initialize the configuration by $this->load->library('upload', $config); it wouldn't do so.

Instead I put my config files in config/upload.php

The other method to do so would have been $this->upload->initialize($config);

查看更多
小情绪 Triste *
3楼-- · 2019-02-04 08:26

Ok put your upload folder in on the original place as you show in your question and try this (give the relative path to your 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);
}
查看更多
孤傲高冷的网名
4楼-- · 2019-02-04 08:39
if ( ! $this->upload->do_upload())


try adding the file field name


if ( ! $this->upload->do_upload('userfile'))
查看更多
登录 后发表回答