简体   繁体   中英

How to save image in folder using CodeIgniter?

i am new to the CodeIgniter.I want to know how to save image in folder.But i was write the code like image name was stored in table.But i want to store the image in folder and retrieve image from the folder.Here i am using the code to store image name in table.

In Controller:

public function product()
{
    $this->load->library('form_validation');
    $this->form_validation->set_rules('productname','Product Code','trim|required');
    $this->form_validation->set_rules('productcode','Product Code','trim|required');
    $this->form_validation->set_rules('productprice','Product Price','trim|required');
    $this->form_validation->set_rules('quantity','Quantity','trim|required');
    $this->form_validation->set_rules('uploadimage','Upload Image','trim_rquired');
    if($this->form_validation->run()==FALSE)
    {
        $this->index();
    }else
        {
            $data['query']=$this->main_model->product_db();
            $this->load->view('query_view',$data);
        }
}

In Model:

public function product_db()
{
    $data=array(
    'productname'=>$this->input->post('productname'),
    'productcode'=>$this->input->post('productcode'),
    'productprice'=>$this->input->post('productprice'),
    'quantity'=>$this->input->post('quantity'),
    'image'=>$this->input->post('uploadimage')
        );
        $query=$this->db->get("product");
        if($query->num_rows())
        {
        $this->db->insert('product',$data);

        $query=$this->db->get("product");
        $this->session->set_userdata($data);
        return $query->result();

    } 
return false;
 }

In View:(form page)

<?php echo validation_errors('<p class="error">'); ?>
<?php echo form_open("main/product"); ?>
    <p>
        <label for="product_name">Product Name:</label>
        <input type="text" id="productname" name="productname" value="<?php echo set_value('product_name'); ?>" />
    </p>        


    <p>
        <label for="ProductCode">Product Code</label>
        <input type="text" id="productcode" name="productcode" value="<?php echo set_value('productcode'); ?>" />
    </p>
    <p>
        <label for="productprice">Product Price:</label>
        <input type="text" id="productprice" name="productprice" value="<?php echo set_value('productprice'); ?>" />
    </p>
    <p>
        <label for="Quantity">Quantity:</label>
        <select name="quantity" id="quantity" value="<?php echo set_value('quantity'); ?>" /><option>1</option>
            <option>2</option>
            <option>3</option>
            <option>4</option>
            <option>5</option>
        </select>

    </p>  
    <p>
        <label for="Uploadimage">Upload Image:</label>
        <input type="file" name="uploadimage" id="uploadimage" value="<?php       echo set_value('quantity'); ?>" />
    </p>

    <p>
        <input type="submit" class="greenButton" value="submit" />
    </p>
<?php echo form_close();
?>   


In View(query_view Page):

 <?php
 echo "<table border='2'>
 <tr>
 <th>productid</th><th>productname</th><th>productcode</th><th>productprice</th>
 <th>quantity</th><th>image</th>           
 </tr>";
 foreach($query as $r)
{
echo "<tr>";    
echo "<td>".$r->productid."</td>"."<td>".$r->productname."</td>".  
    <td>".$r>productcode."</td>"."<td>".$r->productprice."</td>"."<td>"
    .$r->quantity."</td>"."   <td>".$r->image."</td>";
echo "</tr>";
 }
echo "</table>";
echo "<br>";
?>

This is just a sample code of uploading an image:

<?php
    $configUpload['upload_path']    = './uploads/';                 #the folder placed in the root of project
    $configUpload['allowed_types']  = 'gif|jpg|png|bmp|jpeg';       #allowed types description
    $configUpload['max_size']       = '0';                          #max size
    $configUpload['max_width']      = '0';                          #max width
    $configUpload['max_height']     = '0';                          #max height
    $configUpload['encrypt_name']   = true;                         #encrypt name of the uploaded file
    $this->load->library('upload', $configUpload);                  #init the upload class
    if(!$this->upload->do_upload('uploadimage')){
        $uploadedDetails    = $this->upload->display_errors();
    }else{
        $uploadedDetails    = $this->upload->data();    
    }
    print_r($uploadedDetails);die;

?>

you are seeking this kind of code

$config['upload_path'] = './files'; //core folder (if you like upload to application folder use APPPATH)
$config['allowed_types'] = 'gif|jpg|png'; //allowed MIME types
$config['encrypt_name'] = TRUE; //creates uniuque filename this is mainly for security reason

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

if (!$this->upload->do_upload('picture_upload')) { //picture_upload is upload field name set in HTML eg. name="upload_field"
    $error = array('error' => $this->upload->display_errors());
}else{
    //print_r($this->upload->data()); // this is array of uploaded file consist of filename, filepath etc. print it out
    $this->upload->data()['file_name']; // this is how you get for example "file name" of file
}

Please follow this guide http://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html it has everything in it. If you need help with logic its pretty simple

form -> upload field -> button -> form sent -> check rules if file is OK -> upload file -> save data (filename, filepath...) in table.

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