Mitca Vicentiu
Mitca Vicentiu

Reputation: 196

Upload image it's not working codeigniter

I have 2 websites in 1 htdocs directory. The structure's like this

The main directory it's structured like this(name:Quartiere):

Quartiere

Second website directory it's _admin with the same structure(default CodeIgniter structure).

I'm trying to upload a file from _admin to the main directory assets folder, but for some reason, it's not working.I tried other answers from other post but nothing it helped.

What I tried:

  1. Put 'userfile' in do_upload
  2. Checked if the form it's multipart(it is)

What I checked:

  1. If the path it's good(it is)
  2. If the size of the photo it's smaller than 2048(it is)

_admin Controller:

public function add_product(){

        if(!$this->session->userdata('logged_in')){
            redirect('users/login');
        }
        $data['title'] = 'Adauga Produs';
        $data['categories'] = $this->category_model->get_categories();

        $this->form_validation->set_rules('name','Nume','required');
        $this->form_validation->set_rules('category','Categorie','required');
        $this->form_validation->set_rules('ingredients','Ingrediente');
        $this->form_validation->set_rules('price','Pret','required');
        $this->form_validation->set_rules('grams','Gramaj','required');

        if($this->form_validation->run() === FALSE){
            $this->load->view('templates/header');
            $this->load->view('products/adauga', $data);
            $this->load->view('templates/footer');
        }else{
            //upload image
            $config['upload_path'] = 'http://localhost/quartiere/assets/images';
            $config['allowed_types'] = 'jpg|png|jpeg';
            $config['max_size'] = '2048';
            $config['max_width'] = '2000';
            $config['max_height'] = '2000';

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

            if(!$this->upload->do_upload('userfile')){
                $errors = array('error' => $this->upload->display_errors());
                $food_image = 'noimage.png';
            }else{
                $data = array('upload_data' => $this->upload->data());
                $food_image = $_FILES['userfile']['name'];
            }

            $this->food_model->add_product($food_image);
            redirect('produse/adauga');
        }
    }

_admin View:

    <?php echo validation_errors();?>
<?php echo form_open_multipart('products/add_product');?>
<div class="row">
    <div class="col-md-4 col-md-offset-4">
        <h1 class="text-center">
            <?php echo $title; ?>
        </h1>
        <div class="form-group">
            <input type="text" name="name" class="form-control" placeholder="Nume Produs" required/>
        </div>
        <div class="form-group">
            <select class="form-control" name="category" required>
                <?php foreach($categories as $category):?>
                    <option value="<?php echo $category['id'];?>"><?php echo $category['category_name'];?></option>
                <?php endforeach;?>
            </select>
        </div>
        <div class="form-group">
            <label>Bar:</label><br>
            Da <input type="radio" name="bar" class="from-control" value="1"/>
            Nu <input type="radio" name="bar" class="from-control" value="0"/>
        </div>
        <div class="form-group">
            <input type="text" name="ingredients" class="form-control" placeholder="Ingrediente"/>
        </div>
        <div class="form-group">
            <input type="number" name="grams" class="form-control" placeholder="Gramaj"/>
        </div>
        <div class="form-group">
            <label>Poza:</label>
            <input type="file" name="userfile" size="10"/>
        </div>
        <div class="form-group">
            <input type="number" name="price" class="form-control" placeholder="Pret" required/>
        </div>
        <button type="submit" class="btn btn-success btn-block">Adauga Produs</button>
    </div>
</div>
<?php form_close(); ?>

Upvotes: 0

Views: 51

Answers (1)

Karlo Kokkak
Karlo Kokkak

Reputation: 3714

You can make the upload path to - '../assets/images' - or - $_SERVER['DOCUMENT_ROOT'].'/quartiere/assets/images'. Either way is fine.

Upvotes: 2

Related Questions