1

我是 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>";
?>
4

2 回答 2

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;

?>
于 2013-06-26T12:18:18.647 回答
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它包含所有内容。如果您需要逻辑方面的帮助,它非常简单

表单 -> 上传字段 -> 按钮 -> 发送表单 -> 检查文件是否正常 -> 上传文件 -> 将数据(文件名、文件路径...)保存在表中。

于 2013-06-26T08:08:02.663 回答