Welcome Guest, Not a member yet? Register   Sign In
upload not working
#1

[eluser]junaids[/eluser]
Hi,
I am trying to develop a product adding controller. I am trying to insert product data in the database and the product image to be uploaded to the images directory. The data is inserted correctly into the db but the image is not uploaded in the directory. below is my code
VIEW
Code:
<!DOCTYPE HTML>
&lt;html lang="en-US"&gt;
&lt;head&gt;
&lt;title&gt;CI Gallery&lt;/title&gt;
&lt;/head&gt;
&lt;body&gt;
   <div id="blank_gallery">Please Add Product Description</div>

<div id="product_name">
&lt;?php echo form_open('gallery/index'); ?&gt;
    &lt;?php
echo form_input('product_name');
echo form_label('Product Name');
echo form_error('product_name');
?&gt;
    <div id="product_price">
    &lt;?php
echo form_input('product_price');
echo form_label('Product Price');
echo form_error('product_price');
?&gt;
    </div>

<div id="gallery">
   <div id="blank_gallery">Please Upload a Product Image</div>
</div>

<div id="upload">
  &lt;?php
  echo form_open_multipart('gallery');
  echo form_upload('userfile');
  echo form_submit('upload', 'Add Product');
  echo form_close();
  ?&gt;  
</div>
&lt;/body&gt;
&lt;/html&gt;

Here is my Controller
Code:
&lt;?php
class Gallery extends Controller {

function index() {
  $this->load->model('Gallery_model');
  $this->load->library('form_validation');
  $this->form_validation->set_rules('product_name', 'Product Name', 'required');
  $this->form_validation->set_rules('product_price', 'Product Price', 'required|numeric');
  if ($this->form_validation->run() == FALSE)
  {
   $this->load->view('gallery');
  }
  else
  {
  $this->Gallery_model->add_product();
  $this->Gallery_model->do_upload();
  $this->load->view('product_added');
  }
  }
}

and MODEL
Code:
&lt;?php
class Gallery_model extends Model {

var $gallery_path;
var $gallery_path_url;

function Gallery_model() {
  parent::Model();
  
  $this->gallery_path = realpath(APPPATH . '../images');
  $this->gallery_path_url = base_url().'images/';
}

function do_upload() {
  
  $config = array(
   'allowed_types' => 'jpg|jpeg|gif|png',
   'upload_path' => $this->gallery_path,
   'max_size' => 2000
  );
  $this->load->library('upload', $config);
  $this->upload->do_upload();
  $image_data = $this->upload->data();
  
  $config = array(
   'source_image' => $image_data['full_path'],
   'new_image' => $this->gallery_path . '/thumbs',
   'maintain_ration' => true,
   'width' => 150,
   'height' => 100
  );
  
  $this->load->library('image_lib', $config);
  $this->image_lib->resize();
  
}
   function add_product()
          {
         $this->load->database();
  $data = array(
                'name'   => $_POST['product_name'],
  'price'   => $_POST['product_price'],
  );  
                $this->db->insert('products', $data);
          }
}

Need help..Why are the images not uploaded in the directory?
#2

[eluser]junaids[/eluser]
may be in my view, I have opened two forms with form_open() command..one is for the product data and the other for is the form_open_multipart. And I have only one form_close() statement!!!
#3

[eluser]junaids[/eluser]
I added another form_close() statement but it is still not working!!
#4

[eluser]meigwilym[/eluser]
You've presently got two forms, only one of which is submitting (the first) so only this data is being sent to the server.

Combine both into one form, try again then come back with any difficulties.

Mei
#5

[eluser]InsiteFX[/eluser]
Because the directory should be in your root

uploads
application
system

Then your path is './uploads'
#6

[eluser]junaids[/eluser]
@MEI
I have removed the multi_part form and still the image is not uploaded..
@InsiteFX
I have images folder instead of uploads and its in the same directory as you described.
#7

[eluser]meigwilym[/eluser]
You need to change the form tag to multi_part for it to work.

Mei
#8

[eluser]junaids[/eluser]
@mei
Thanx..it helped




Theme © iAndrew 2016 - Forum software by © MyBB