简体   繁体   中英

Codeigniter upload multiple image with thumbnail and add to database

For a website im trying to add multiple product images to a product (add to database) with a thumbnail. I keep getting an error :

  The upload path does not appear to be valid.

I use the exact same code (for the path ) that i use to upload the main image of the product and it works there. my code:

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

i think the part thats most relevant is :

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

i added some code for debugging

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

this returns bool(true) so i guess its definetly a valid path

Why do i still get this error?

Relative Path

Add a constant in index.php and create a path relative path to it.

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

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

Create your link

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

Folder structure

index.php
    |->media
        |->uploads

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM