Welcome Guest, Not a member yet? Register   Sign In
upload image with text
#1

Hi coder,


any help how to upload image together with text.

conroller

PHP Code:
public function do_upload()
   {
    
$config['upload_path'] = './upload/';
    
$config['allowed_types'] = 'gif|jpg|png';
    
$config['max_size'] = '100';
    
$config['max_width' '500';
    
$config['max_height' '500';

    
$this->load->library('upload'$config);
                
    
$this->form_validation->set_rules('item''item''trim|required');
    
$this->form_validation->set_rules('price''price''trim|required');
    
$this->form_validation->set_rules('weight''weight''trim|required');
                
    if(
$this->form_validation->run() == false)
        {
            
$this->load->view('admin');
            return 
false;
        }
    if(! 
$this->upload->do_upload())
        {
            
$error = array('error' => $this->upload->display_errors());
            
$this->load->view('admin'$error);
        }
    else
        {
            
$data = array('upload_data' => $this->upload->data());
            
$item $this->input->post('item');
            
$price $this->input->post('price');
            
$weight $this->input->post('weight');
                        
            
$this->admin_model->act_additem($item$price$weight$data["file_name"]);
                        
            
$this->load->view('admin'$data);
        }
   } 

model
PHP Code:
public function act_additem($item$price$weight$data['file_name'])
    { 
         
$query $this->db->query("INSERT INTO stocks(item,price,weight,img) VALUES('$item','$price','$weight','$data[file_name]')");
    } 
Reply
#2

Hi, what was wrong with this code, i did not get the image file name to insert, while text file are getting to insert well.

PHP Code:
public function do_upload()
            {
                
$config['upload_path'] = 'upload/';
                
$config['allowed_types'] = 'gif|jpg|png';
                
$config['max_size']    = '100';
                
$config['max_width' '1024';
                
$config['max_height' '768';

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

                
$upload_data $this->upload->data(); 
                
$file_name   $upload_data['file_name'];

                
$item $this->input->post('item');
                
$price $this->input->post('price');
                
$weight $this->input->post('weight');
                        
                
$data['result'] = $this->admin_model->act_additem($item$price$weight$file_name);
                        
                
$this->load->view('admin');
                
            } 
Reply
#3

(This post was last modified: 11-26-2014, 01:51 AM by sv3tli0.)

Your model function is wrong.. File name should be a variable not element from array.. 

And I can suggest to you to use DB insert method from CI db activerecords

PHP Code:
   public function act_additem($item$price$weight$file_name)
   {
        $data = array(
 
           'item'   => $item,
 
           'price'  => $price,
 
           'weight' => $weight,
 
           'img'    => $file_name
        
);
 
       return (bool) $this->db->insert('stocks'$data); 
   

Here is and one little update of the controller method I can suggest to you..
PHP Code:
   public function do_upload()
 
   {
 
       $config['upload_path'] = './upload/';
 
       $config['allowed_types'] = 'gif|jpg|png';
 
       $config['max_size'] = '100';
 
       $config['max_width' '500';
 
       $config['max_height' '500';

 
       $this->load->library('upload'$config);
 
       
        $this
->form_validation->set_rules('item''item''trim|required');
 
       $this->form_validation->set_rules('price''price''trim|required');
 
       $this->form_validation->set_rules('weight''weight''trim|required');
 
       
        $data 
= array();

 
       if$this->form_validation->run() !== false)
 
       {
 
           $data['error'] = 'Invalid form data..'// You can use and form-validation messages inside your view
 
       }

 
       if( ! isset($data['error']) && ! $this->upload->do_upload())
 
       {
 
           $data['error'] = $this->upload->display_errors();
 
       }

 
       if( ! isset($data['error'])) {
 
           $data = array('upload_data' => $this->upload->data());
 
           $item $this->input->post('item');
 
           $price $this->input->post('price');
 
           $weight $this->input->post('weight');
 
           
            if
( ! $this->admin_model->act_additem($item$price$weight$data["upload_data"]["file_name"])) {
 
               // Either add error or fully replace the return $data
 
               // It can be a good idea to delete the uploaded file if its information is not stored at DB
 
               $data['error'] = "Unable to save it inside our DB!";
 
           }
 
           else
            
{
 
               // Some param just to know that everything went well..
 
               $data['success'] = "You upload was successful!";
 
           }
 
       }


 
       return $this->load->view('admin'$data);
 
   

P.S. I forgot to mention one important thing:

$data["file_name"] - this is not valid because you are storing the upload array inside $data["upload_data"]..
Use $data["upload_data"]["file_name"] instead!
Best VPS Hosting : Digital Ocean
Reply
#4

(This post was last modified: 11-26-2014, 01:39 AM by jaysondotp.)

Thanks for your help but still same, its still did not get the file name of an image.. i think the problem is here
PHP Code:
$upload_data $this->upload->data(); 
     
$file_name =   $upload_data['file_name']; 
but i have no idea, how to get it correct.
Reply
#5

Hi sv3tli0 for your help, im getting near to a real.

still not work and here is my view.. and i don't know where is the problem..
PHP Code:
<form action "<?php echo base_url();?>index.php/admin/do_upload" method "post" name="create_po">
    
    <
input type="text" name "item" value "" class="form-control" id="new_textsz"  required="required">
    <
input type="text" name "price" value ""class="form-control" id="new_textsz"  required="required">
    <
input type="text" name "weight" value ""class="form-control" id="new_textsz"  required="required">
    <
input type="file" name="image" /><br />
<
button class="btn btn-primary type="submit" name = "submit">Save</button>
</form> 
Reply
#6

(This post was last modified: 11-26-2014, 02:13 AM by sv3tli0.)

I forgot one thing:
Set the field name at do_upload as default is userfile I think..

PHP Code:
$this->upload->do_upload('image'


And from your view I see that you are missing 1 " after button class's btn btn-primary and more important is that your form element must have:
Code:
enctype="multipart/form-data"
Best VPS Hosting : Digital Ocean
Reply




Theme © iAndrew 2016 - Forum software by © MyBB