Codeigniter使用缩略图上传多个图像并添加到数据库

时间:2014-05-02 13:08:06

标签: php codeigniter

对于尝试使用缩略图将多个产品图像添加到产品(添加到数据库)的网站。我一直收到错误:

  The upload path does not appear to be valid.

我使用完全相同的代码(用于路径),我用它来上传产品的主图像,并在那里工作。 我的代码:

public function add_imgs($id)
{

    $data['artikel'] = $this->artikel_model->get_artikel($id); //for the filenames
    $data['error'] = "";    
    $this->load->library('upload');
    $files = $_FILES;
    $aantal = count($_FILES['userfile']['name']);
    for($i=0; $i<$aantal; $i++)
    {

        $_FILES['userfile']['name']= $files['userfile']['name'][$i];
        $_FILES['userfile']['type']= $files['userfile']['type'][$i];
        $_FILES['userfile']['tmp_name']= $files['userfile']['tmp_name'][$i];
        $_FILES['userfile']['error']= $files['userfile']['error'][$i];
        $_FILES['userfile']['size']= $files['userfile']['size'][$i];    



        $config['upload_path'] = $_SERVER['DOCUMENT_ROOT'].'/assets/img/test/';

        $config['allowed_types'] = 'gif|jpg|png';
        $config['max_size'] = '10000';
        $config['file_name']    = $data['artikel']['titel'].$data['artikel']['id'];
        $config['overwrite'] = FALSE;

        $this->load->library('upload', $config);
        if ( ! $this->upload->do_upload())
        {
            $data['error'] .= $this->upload->display_errors();
            echo $data['error'];
            var_dump(is_dir( $config['upload_path'])); 
            return;
        }
        else
        {
            $data['artikel'] = $this->artikel_model->get_artikel($id);  
            $imgdata = $this->upload->data();
            $filename = $imgdata['file_name'];
            $fullpath = $imgdata['full_path'];
            $this->artikel_model->add_image($filename, $id);


            $this->load->library('image_lib');
            $config['source_image'] = $fullpath;
            $config['image_library'] = 'gd2';
            $config['maintain_ratio'] = FALSE;
            $config['width']     = 400; 
            $config['height']   = 400;
            $this->image_lib->initialize($config);

            if ( ! $this->image_lib->resize())
            {
                echo $config['source_image'];
                echo $this->image_lib->display_errors();
            }

            $this->image_lib->clear();

            $this->load->library('image_lib');
            $config['source_image'] = $fullpath;
            $config['image_library'] = 'gd2';
            $config['maintain_ratio'] = FALSE;
            $config['width']     = 180; 
            $config['new_image'] = $_SERVER['DOCUMENT_ROOT'].'/assets/img/artikel-thumb/'.$filename;
            $config['height']   = 180;
            $this->image_lib->initialize($config);
            if ( ! $this->image_lib->resize())
            {
                echo $config['source_image'];
                echo $this->image_lib->display_errors();
            }

            $this->image_lib->clear();          
        }


    }
    $this->load->model('collectie_model');
    $this->load->helper(array('url','form'));
    $this->load->library('form_validation');
    $data['collecties'] = $this->collectie_model->get_collecties_all();
    $data['afbeeldingen'] = $this->artikel_model->get_afbeeldingen($id);
    $data['title'] = 'Artikel : '.$data['artikel']['titel'];
    $this->load->view('templates/header', $data);
    $this->load->view('admin/artikelen/view', $data);  
    $this->load->view('templates/footeradmin');
}

我认为最相关的部分是:

     $config['upload_path'] = $_SERVER['DOCUMENT_ROOT'].'/assets/img/test/';

我添加了一些调试代码

      if ( ! $this->upload->do_upload())
        {
            $data['error'] .= $this->upload->display_errors();
            echo $data['error'];
            var_dump(is_dir( $config['upload_path'])); <--- this return bool(true) 
            return;
        }

这会返回bool(true),所以我猜它肯定是一个有效的路径

为什么我仍然会收到此错误?

1 个答案:

答案 0 :(得分:0)

相对路径

index.php 中添加常量 并为其创建路径相对路径

//If PHP > 5.3
define('MEDIA', __DIR__ . '/media/');

//If PHP < 5.3
define('MEDIA', dirname(__FILE__) . '/media/');

创建链接

$config['upload_path'] = MEDIA . 'uploads';

文件夹结构

index.php
    |->media
        |->uploads