It's ok to upload image in file using codeigniter upload library with this example.
However, I want to send filename in database. Can anybody pls give me example ? Thanks
<?php
class Upload extends Controller {
function Upload()
{
parent::Controller();
$this->load->helper(array('form', 'url'));
}
function index()
{
$this->load->view('upload_form', array('error' => ' ' ));
}
function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $开发者_如何转开发data);
}
}
}
?>
Or do we have any alternative?
You can get it with data()
method like this:
$upload_data = $this->upload->data();
echo $upload_data['file_name'];
Note that you can also get other upload data, just check what you have:
print_r($upload_data);
Create an array with whatever you need to upload in your db...and then insert it in the db put something like this under else:
//create array to load to database
$insert_data = array(
'name' => $image_data['file_name'],
'path' => $image_data['full_path'],
'thumb_path'=> $image_data['file_path'] . 'thumbs/'. $image_data['file_name'],
'tag' => $tag
);
$this->db->insert('photos', $insert_data);//load array to database
Important:the indexes of the array got to have same name with the columns of your table!
also try and use your model it will make your code a bit easier to maintain,read etc best of luck!
you can use this way :
$data = array('upload' => $this->upload->data());
echo $data['upload']['file_name'];
$config['upload_path'] = './assets/uploads/'; // Upload Path will be here
$config['allowed_types'] = 'gif|jpg|png';
$this->upload->initialize($config);
if ($this->upload->do_upload('upload')) {
$file_data = $this->upload->data();
$file_name= $file_data['file_name']; //get file name of your uploaded file from here..
}
else {
echo "Error";
}
$data = array(
'image_name' => $file_data['file_name'], //insert your image name from here..
);
$this->add_model->image_upload($data); //that will go to the model function..
}
精彩评论