如何使用CodeIgniter在文件夹中保存图像?

时间:2013-06-26 07:26:54

标签: php html mysql codeigniter

我是CodeIgniter的新手。我想知道如何将文件保存在文件夹中。但我写的是像图像名称的代码存储在表格中。但是我想将图像存储在文件夹中并从文件夹中检索图像。我在使用代码将图像名称存储在表格中。

在控制器中:

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);
        }
}

在模特:

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;
 }

在视图中:(表单页面)

<?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 个答案:

答案 0 :(得分:3)

这只是上传图片的示例代码:

<?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;

?>

答案 1 :(得分:0)

你正在寻找这种代码

$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
}

请按照本指南http://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html了解其中的所有内容。如果您需要逻辑帮助,那么非常简单

表格 - &gt;上传字段 - &gt;按钮 - &gt;表单已发送 - &gt;检查文件是否正常 - &gt;上传文件 - &gt;在表格中保存数据(文件名,文件路径...)。