【问题标题】:"The upload path does not appear to be valid". Codeigniter file upload not working [closed]“上传路径似乎无效”。 Codeigniter文件上传不起作用[关闭]
【发布时间】:2012-01-29 16:31:14
【问题描述】:

我在这一点上卡了很长时间,即使在阅读了很多帖子之后,我也无法找到解决方案。

我正在制作一个测验的界面,在管理界面中,我需要将图像上传到文件夹并将图像名称上传到数据库。我可以处理其他事情,但图片上传困扰了我很长时间。

请看我下面的代码。

形式

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

我试过运行一个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');
}

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

我不知道我哪里错了。

更新

我的目录结构是

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

我正在操作一台 Windows 机器并使用 WAMP。这有什么不同吗?

【问题讨论】:

  • 你试过把上传文件夹放在应用程序和公共文件夹中
  • @Hemant 将上传文件夹放在应用程序中会使其在逻辑上无法访问。是的,我曾尝试将其公开,但没有成功。
  • 尝试从 $config['upload_path'] 中删除 base_url()

标签: codeigniter file-upload


【解决方案1】:

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

我把我的配置文件放在config/upload.php

另一种方法是$this-&gt;upload-&gt;initialize($config);

【讨论】:

  • 哇,将我的配置移到 config/upload.php 解决了我的问题!
  • 解决了同样的问题:$this->upload->initialize($config);
  • @ViperTecPro 嗨,我遇到了同样的错误,我尝试了该解决方案但没有用
  • 对我来说,我的上传文件夹被 git revert =)) 删除了
  • 别在意 linux 中的 chmod 777 upload_folder
【解决方案2】:

好的,将您的上传文件夹放在您在问题中显示的原始位置并尝试此操作(提供您的 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);
}

【讨论】:

    【解决方案3】:
    if ( ! $this->upload->do_upload())
    
    
    try adding the file field name
    
    
    if ( ! $this->upload->do_upload('userfile'))
    

    【讨论】:

    • 这没有什么区别,因为错误指向我的上传路径而不是字段。
    • ok open config/constants.php define('UPLOADS', APPPATH.'uploads/'); $config['upload_path'] = 上传;
    • 不,@Philip,我试过了,但它在 CI3 中不起作用
    猜你喜欢
    • 2013-05-17
    • 2018-09-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-12-28
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多