Php 如何使用CodeIgniter将图像保存在文件夹中?
我是CodeIgniter的新手。我想知道如何在文件夹中保存图像。但我编写的代码类似于存储在表中的图像名称。但我想将图像存储在一个文件夹中,并从该文件夹中检索图像。在这里,我使用代码将图像名称存储在表中: 在控制器中:Php 如何使用CodeIgniter将图像保存在文件夹中?,php,html,mysql,codeigniter,Php,Html,Mysql,Codeigniter,我是CodeIgniter的新手。我想知道如何在文件夹中保存图像。但我编写的代码类似于存储在表中的图像名称。但我想将图像存储在一个文件夹中,并从该文件夹中检索图像。在这里,我使用代码将图像名称存储在表中: 在控制器中: public function product() { $this->load->library('form_validation'); $this->form_validation->set_rules('productname','Pr
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;
}
视图:(表单页)
产品名称:
您正在寻找这种代码
$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
}
请按照这本指南去做,里面什么都有。如果你需要逻辑方面的帮助,那很简单
表单->上传字段->按钮->表单发送->检查文件是否正常->上传文件->在表中保存数据(文件名、文件路径…)。这只是上传图像的示例代码:
<?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;
?>
您的文件上载代码在哪里,您将无法在post$this->input->post('uploadimage')
中获得图像数据,而您必须使用$\u文件
也请参考其他问题和答案
<?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;
?>