dongzhuang6177 2013-06-26 07:26 采纳率: 100%
浏览 37

如何使用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>";
?>
  • 写回答

2条回答 默认 最新

  • dousi5501 2013-06-26 08:08
    关注

    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.

    评论

报告相同问题?