Codeigniter上传图片 - 上传路径似乎无效

时间:2011-11-14 12:12:21

标签: php codeigniter file-upload

我在使用codeigniter上传库上传图片时遇到问题,我一直收到错误,

  

上传路径似乎无效。

我尝试上传的文件路径如下,

$config['upload_path'] = './media/images/site';

当我点下面的内容时,

die(var_dump(is_dir($config['upload_path'])));

我收到以下内容,

  

BOOL(真)

该文件夹也是可写的(777),所以我不确定我哪里出错了,下面是我的上传代码,

$config['upload_path'] = './media/images/site';
        //die(realpath($config['upload_path']));
        //die(var_dump(is_dir($config['upload_path'])));  
        $config['allowed_types'] = 'gif|jpg|png|jpeg';
        $config['max_size'] = '1500';
        $config['max_width']  = '390';
        $config['max_height']  = '269';

        $this->load->library('upload',$config);
        if ( ! $this->upload->do_upload() && $_FILES['userfile']['error'] != 4)
        {
            $data['error'] = $this->upload->display_errors();
            $this->template->build('/admin/pages/about_us', $data);
        }
        else
        {
            $image = $this->upload->data();
            if(empty($image['file_name'])) {
                $image['file_name'] = $this->formbuilder->defaults['page_image'];
            }
            $page = array(
                //'page_title' => $this->input->post('page_title'),
                //'page_slug' => $this->input->post('page_slug'),
                'page_content' => $this->input->post('page_content'),
                'page_image' => $image['file_name'],
                'date_created' => date("Y-m-d h:i:s"),
                'created_by' => 1
            );

            if($this->pages_model->insert($this->security->xss_clean($page)))
            {
                redirect('/admin/dashboard');
            }
        }

我看不到任何明显会阻止文件上传的内容,下面是我项目的文件夹结构

/application
/system 
index.php
.htaccess
/media
    /images
        /site
        /admin
    /css
    /js

如果有人能够解决我的问题,那就太棒了。

为PETULENT DOWNVOTER编辑

导致此问题的原因是什么?

(有一个问题,好像它还不够明显)

3 个答案:

答案 0 :(得分:8)

尝试在libary加载行之前添加此行:

 // Alternately you can set preferences by calling the initialize function. Useful if you auto-load the class:
    $this->upload->initialize($config);

不要忘记在模板中添加此行:

<?php echo form_open_multipart('upload/do_upload');?>

答案 1 :(得分:0)

您的问题似乎发生在validate_upload_path

system/libraries/Upload.php方法中

这种方法可以解决你的上传路径到完整路径的各种技巧,所以我建议在每行之前插入一个var转储,如下所示:  $this->set_error('upload_no_filepath'); 确定它出错的地方,然后从那里回溯。

答案 2 :(得分:0)

控制器

中试用此代码
$config['upload_path'] = 'images'; //name of the uploading folder
$config['allowed_types'] = 'jpeg|png|jpg'; 

$this->load->library('upload', $config);
$this->upload->initialize($config);

if (!$this->upload->do_upload())
 {
  echo $this->upload->display_errors();
  exit;
 }
else
 {
  $upload_data = $this->upload->data();
 }