Upload Query Codeigniter
I have done the below code and I now get a database error -> Still cannot seem to get it to upload:
Error:
Column 'image_path' cannot be null
Database Structure
Controller:
class Addsale extends CI_Controller {
function __construct(){
parent::__construct();
}
function index() {
if(!$this->session->userdata('logged_in')) {
redirect('admin/home');
}
// Main Page Data
$data['cms_pages'] = $this->navigation_model->getCMSPages();
$data['title'] = 'Add Sale';
$data['content'] = $this->load->view('admin/addsale', $data);
$this->load->view('admintemplate', $data);
//Set Validation
$this->form_validation->set_rules('name', 'Name', 'trim|required');
$this->form_validation->set_rules('location', 'Location', 'trim|required');
$this->form_validation->set_rules('bedrooms', 'Bedrooms', 'trim|is_natural|required');
$this->form_validation->set_rules('bathrooms', 'Bathrooms', 'trim|required');
$this->form_validation->set_rules('condition', 'Condition', 'trim|required');
$this->form_validation->set_rules('description', 'Description', 'trim|required');
$this->form_validation->set_rules('price', 'Price', 'trim|required');
if($this->form_validation->run() === TRUE) {
$this->load->library('upload', $config);
$file_info = $this->upload->do_upload();
$data = array(
'name' => $this->input->post('name', TRUE),
'location' => $this->input->post('location', TRUE),
'bedrooms' => $this->input->post('bedrooms', TRUE),
'bathrooms' => $this->input->post('bathrooms', TRUE),
'condition' => $this->input->post('condition', TRUE),
'description' => $this->input->post('description', TRUE),
'price' => $this->input->post('price', TRUE),
'image_path' => $file_info['full_path']
);
$this->sales_model->addSale($data);
}
}
function do_upload(){
//Set File Settings
$config['upload_path'] = './includes/uploads/';
$config['allowed_types'] = 'jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
}
}
Model:
function addSale($data) {
$this->db->insert('sales', $data);
return;
}
Original Code:
Hello,
I have the following section of code:
if($this->form_validation->run() === TRUE) {
$data = array(
'name' => $this->input->post('name', TRUE),
'location' => $this->input->post('location', TRUE),
'bedrooms' => $this->input->post('bedrooms', TRUE),
'bathrooms' => $this->input->post('bathrooms', TRUE),
'condition' => $this->input->post('condition', T开发者_如何学运维RUE),
'description' => $this->input->post('description', TRUE),
'price' => $this->input->post('price', TRUE),
'image_path' => $this->input->post('userfile', TRUE)
);
$this->load->library('upload', $config);
$this->upload->do_upload();
}
}
What I am trying to do is when the form is valid save the data to the database (works fine) and upload the image.
Were I am struggling is that I cannot get the userfile "name" to save and the file will not upload.
Can this be done within the index function?
Files that are uploaded don't get included in post at all, so you can't get the name in the way you want it. You'll have to do something like this instead:
if($this->form_validation->run() === TRUE) {
$this->load->library('upload', $config);
$file_info = $this->upload->do_upload();
$data = array(
'name' => $this->input->post('name', TRUE),
'location' => $this->input->post('location', TRUE),
'bedrooms' => $this->input->post('bedrooms', TRUE),
'bathrooms' => $this->input->post('bathrooms', TRUE),
'condition' => $this->input->post('condition', TRUE),
'description' => $this->input->post('description', TRUE),
'price' => $this->input->post('price', TRUE),
'image_path' => $file_info['full_path']
);
}
This will put the full path into your database if you just want the file name use 'client_name' or something (see Ross' answer for reference)
The function $this->upload->do_upload()
returns an array of information about the file uploaded.
So:
$upload_data = $this->upload->do_upload();
print_r($upload_data); // to see everything
Array
(
[file_name] => mypic.jpg
[file_type] => image/jpeg
[file_path] => /path/to/your/upload/
[full_path] => /path/to/your/upload/jpg.jpg
[raw_name] => mypic
[orig_name] => mypic.jpg
[client_name] => mypic.jpg
[file_ext] => .jpg
[file_size] => 22.2
[is_image] => 1
[image_width] => 800
[image_height] => 600
[image_type] => jpeg
[image_size_str] => width="800" height="200"
)
So, depending on the value you need, you will want to do the upload first, and if it is successful, continue with processing the POST data. Then you can do this in your $data
array:
'image_path' => $upload_data['file_name']; // adjust value to get what you want.
and you should be good to go.
精彩评论