In my page , i have created one form with different fields like name, email,password,etc., now i have to upload the image and store in my database. any one send the code with all fields including image upload file. i have to try in many times, image can't upload in my database.
my controller is
function activity()
{
$this->load->library('form_validation');
//field name,error message, validation rules
$this->form_validation->set_rules('activityid', 'Activity Id', 'trim|required');
$this->form_validation->set_rules('hostid', 'Host Id', 'trim|required');
$this->form_validation->set_rules('activityname', 'Activity Name', 'trim|required');
$this->form_validation->set_rules('date', 'Date', 'trim|required');
$this->form_validation->set_rules('venue', 'Venue', 'trim|required');
$this->form_validation->set_rules('typeofactivity', 'Type of Activity', 'trim|required');
$this->form_validation->set_rules('conductedby', 'Conducted By', 'trim|required');
$config['upload_path'] = './asset/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '1000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
$w = $this->upload->data();
if($this->form_validation->run()==FALSE)
{
//$this->load->view('signup_form');
$this->activity_reg();
}
else
{
$this->load->model('membership_model');
$query=$this->membership_model->activity();
if($query)
{
$data['main_content']='signup_successful';
$this->load->view('includes/templates',$data);
}
else
{
$this->load->view('activity');
}
}
}
my model is
function activity()
{
$w = $this->upload->data();
$new_member_insert_data= array(
'activityid'=>$this->input->post('activityid'),
'hostid'=>$this->input->post('hostid'),
'activityname'=>$this->input->post('activityname'),
'date'=>$this->input->post('date'),
'venue'=>$this->input->post('venue'),
'typeofactivity'=>$this->input->post('typeofactivity'),
'conductedby'=>$this->input->post('conductedby'),
'image' => $w['file_name']
);
$this->load->database();
$insert=$this->db->insert('activity',$new_member_insert_data);
return $insert;
}
Use file_get_contents() function.
$image = $_FILES['YOUR_INPUT_NAME']['tmp_name'];
$new_member_insert_data= array(
'activityid'=>$this->input->post('activityid'),
'hostid'=>$this->input->post('hostid'),
'activityname'=>$this->input->post('activityname'),
'date'=>$this->input->post('date'),
'venue'=>$this->input->post('venue'),
'typeofactivity'=>$this->input->post('typeofactivity'),
'conductedby'=>$this->input->post('conductedby'),
'image' => file_get_contents( $image )
);
Related
I have a problem with multiple image upload in codeigniter. I want to insert to each field in database.
I've tried to upload single a file but when I change to multiple upload, it's not working.
This is my controller to add the whole data from upload images:
public function addprod(){
$item->gambar_produk = null;
$item->thumb_produk1 = null;
$data = array(
'page' => 'addprod',
'row' => $item
);
$data['title'] = 'Form Add';
$this->load->view('admin/addprod', $data);
}
This is My Controller to add images and this is my first images controller:
$config['upload_path'] = './assets/upload/images/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '2400';
$config['max_width'] = '2024';
$config['max_height'] = '2024';
$this->load->library('upload', $config);
if ($this->upload->do_upload('image')){
$post['image'] = $this->upload->data('file_name');
$this->Model_product->add($post);
redirect('Product/viewprod');
} else {
$error = $this->upload->display_errors();
$this->session->flashdata('error', $error);
redirect('Product/addprod');
}
and this is my second images:
$config2['upload_path'] = './assets/upload/images/';
$config2['allowed_types'] = 'gif|jpg|png|jpeg';
$config2['max_size'] = '2400';
$config2['max_width'] = '2024';
$config2['max_height'] = '2024';
$this->load->library('upload', $config2);
if ($this->upload->do_upload('image2')){
$post['image2'] = $this->upload->data('file_name');
$this->Model_product->add($post);
redirect('Product/viewprod');
} else {
redirect('Product/addprod');
}
this is my model:
public function add($post){
$data = [
'image' => $post['image'],
'image2' => $post['image2']
];
$this->db->insert('tb_prod',$data);
}
I am new to CI and I am learning it through tutorials on Youtube. I am trying to upload image with what I found on tutorial here but unfortunately its not working. Below are the codes please help me.
Controller
public function create() {
$data['title'] = 'Create Post';
$data['desc'] = 'Feel free to create a new post here.';
$data['categories'] = $this->post_model->get_categories();
$this->form_validation->set_rules('title', 'Post Title', 'required');
$this->form_validation->set_rules('desc', 'Post Description', 'required');
if($this->form_validation->run() == FALSE) {
$this->load->view('templates/header');
$this->load->view('posts/create', $data);
$this->load->view('templates/footer');
} else {
// Upload Image
$config['upload_path'] = './assets/images/posts';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 2048;
$config['max_width'] = 500;
$config['max_height'] = 500;
$this->upload->initialize($config);
if(!$this->upload->do_upload()){
$errors = array('error' => $this->upload->display_errors());
$post_image = 'noimage.jpg';
} else {
$data = array('upload_data' => $this->upload->data());
$post_image = $_FILES['userfile']['name'];
}
$this->post_model->create_post($post_image);
return redirect('posts');
}
}
Model
public function create_post($post_image) {
$slug = url_title($this->input->post('title'));
$data = array(
'post_title' => $this->input->post('title'),
'post_slug' => $slug,
'post_desc' => $this->input->post('desc'),
'post_cat_id' => $this->input->post('catid'),
'post_image' => $post_image
);
return $this->db->insert('posts', $data);
}
View
<?php echo form_open_multipart('posts/create'); ?>
<div class="form-group">
<label>Post Title</label>
<?php echo form_input(['name'=>'title', 'placeholder'=>'Add Title', 'class'=>'form-control', 'value'=>set_value('title')]); ?>
</div>
<div class="form-group">
<label>Select Category</label>
<select name="catid" class="form-control">
<?php foreach($categories as $category): ?>
<option value="<?php echo $category['cat_id']; ?>"><?php echo $category['cat_name']; ?></option>
<?php endforeach; ?>
</select>
</div>
<div class="form-group">
<label>Post Description</label>
<?php echo form_textarea(['name'=>'desc', 'placeholder'=>'Add Description', 'class'=>'form-control', 'value'=>set_value('desc')]); ?>
</div>
<div class="form-group">
<label>Upload Image</label>
<input type="file" name="userfile" size="20">
</div>
<input type="submit" class="btn btn-primary" value="Add Post">
Now whenever I try to create a post with an image it executes if(!$this->upload->do_upload()){ as TRUE and thus inserts noimage.jpg in database as well as image is not uploaded too. Any help would be appreciated. Thanks.
EDIT
On trying to DEBUG it using print_r($this->upload->data()); die(); I got an empty list of array which means the file is not getting submitted from the form itself on the first place. But why? I don't find any error in the form.
Array
(
[file_name] =>
[file_type] =>
[file_path] => ./assets/images/posts/
[full_path] => ./assets/images/posts/
[raw_name] =>
[orig_name] =>
[client_name] =>
[file_ext] =>
[file_size] =>
[is_image] =>
[image_width] =>
[image_height] =>
[image_type] =>
[image_size_str] =>
)
and print_r($errors) give this error. I am close to solve now I guess.
Array
(
[error] =>
The image you are attempting to upload doesn't fit into the allowed dimensions.
)
SOLUTION
I had to change the max_width and max_height to 0 for unlimited. The image was not being uploaded due to this error.
If you think your code is correct. Have you checked folder permissions?
You have to give permission to folders in order to store images
Forget to load upload library do like this :
$this->load->library('upload', $config);
The whole code (file uploading part) should be like this :
else {
// Upload Image
$config['upload_path'] = FCPATH.'assets/images/posts';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 2048;
$config['max_width'] = 500;
$config['max_height'] = 500;
$this->load->library('upload', $config);
if($this->upload->do_upload('userfile'))
{
print_r($this->upload->data());die;
$file_name = $this->upload->data('file_name');
$post_image = $file_name;
}
else
{
$errors = array('error' => $this->upload->display_errors());
print_r($errors);die;
$post_image = 'noimage.jpg';
}
$this->post_model->create_post($post_image);
return redirect('posts');
}
try following changes
$config['upload_path'] = './assets/images/posts'; **to** $config['upload_path'] = getcwd().'/assets/images/posts';
You must remove library upload from autoloading and edit code like this.
if($this->form_validation->run() == FALSE) {
$this->load->view('templates/header');
$this->load->view('posts/create', $data);
$this->load->view('templates/footer');
} else {
// Upload Image
$config['upload_path'] = './assets/images/posts';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 2048;
$config['max_width'] = 500;
$config['max_height'] = 500;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if(!$this->upload->do_upload('userfile')){
$errors = array('error' => $this->upload->display_errors());
$post_image = 'noimage.jpg';
} else {
$data = array('upload_data' => $this->upload->data());
$post_image = $_FILES['userfile']['name'];
}
$this->post_model->create_post($post_image);
return redirect('posts');
}
try this code!
public function create() {
$data['title'] = 'Create Post';
$data['desc'] = 'Feel free to create a new post here.';
$data['categories'] = $this->post_model->get_categories();
$this->form_validation->set_rules('title', 'Post Title', 'required');
$this->form_validation->set_rules('desc', 'Post Description', 'required');
if($this->form_validation->run() == FALSE) {
$this->load->view('templates/header');
$this->load->view('posts/create', $data);
$this->load->view('templates/footer');
} else {
// Upload Image
$config['upload_path'] = './assets/images/posts';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 2048;
$config['max_width'] = 500;
$config['max_height'] = 500;
$this->load->library('upload');
$this->upload->initialize($config);
if(!$this->upload->do_upload('userfile'))
{
$errors = array('error' => $this->upload->display_errors());
$post_image = 'noimage.jpg';
} else {
$data = array('upload_data' => $this->upload->data());
$post_image = $_FILES['userfile']['name'];
}
$this->post_model->create_post($post_image);
return redirect('posts');
}
I'm trying to upload a zip files but I'm getting some error and I want to display the images inside of it. This is my code this is my code:
Controller
$config['upload_path'] = './img/header/';
$config['allowed_types'] = 'rar|zip|gif|jpg|png|jpeg';
$config['max_size'] = '2048000';
$config['overwrite'] = TRUE;
$this->load->library('upload', $config);
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="alert alert-danger" role="alert">', '</div>');
$this->form_validation->set_rules('title', 'Name', 'trim|required');
if ($this->form_validation->run() == FALSE || !$this->upload->do_upload('userfile')) {
$this->load->view('plmar/admin/headContent');
} else {
$content = array(
'header_title' => $_POST['title'],
'header_path' => $_POST['userfile'],
'upload_data' => $this->upload->data()
);
$this->adminModel->addContent($content);
redirect('Administrator/headContent');
}
$this->load->view('admin/footer');
just add *
for Ex : $config['allowed_types'] = '*'; and remove this $config['allowed_types'] = 'rar|zip|gif|jpg|png|jpeg';
public function add_software()
{
$this->load->library('form_validation');
$this->form_validation->set_error_delimiters('<div class="error-form">',
'</div>');
$this->form_validation->set_rules('name', 'Name',
'required|min_length[4]|max_length[25]');
$this->form_validation->set_rules('desc', 'Description', 'required');
$this->form_validation->set_rules('licence', 'Licence.', 'required');
$this->form_validation->set_rules('platform', 'Platform', 'required');
$this->form_validation->set_rules('developers', 'Developer',
'required');
$this->form_validation->set_rules('link', 'Developer Website',
'required');
$this->form_validation->set_rules('cat', 'Category', 'required');
$this->form_validation->set_rules('logo', 'Software Logo', 'required');
if ($this->form_validation->run() == FALSE) {
$this->load->view('admin/includes/header');
$this->load->view('admin/add_software');
$this->load->view('admin/includes/footer');
} else {
//Setting values for tabel columns
$data['name'] = $this->input->post('name');
$data['description'] = $this->input->post('desc');
$data['licence'] = $this->input->post('licence');
$data['platform'] = $this->input->post('platform');
$data['developers'] = $this->input->post('developers');
$data['link'] = $this->input->post('link');
$data['category'] = $this->input->post('cat');
$data['logo'] = $this->input->post('logo');
/////////////////////////////////
//set preferences
$config = array(
'upload_path' => '' .base_url(). '/assets/img/logo/',
'allowed_types' => "gif|jpg|png|jpeg|pdf",
'overwrite' => TRUE,
'max_size' => "4096000",
'max_height' => "1024",
'max_width' => "1024"
);
//load upload class library
$this->load->library('upload', $config);
$this->upload->data();
$this->upload->do_upload();
/////////////////////////////////
//Transfering data to Model
$this->insert_model->form_insert($data);
$data['message'] = 'Data Inserted Successfully';
//Loading View
redirect('admin/view_softwares', 'refresh');
}
}
This is my codeigniter controller function. Using this function, I can add data to the database. But, I am unable to upload image. Even upload_files is enabled in php.ini. I tried resolving this problem all day long, but all in vain.
I have not used enctype in my html form because when I use enctype, I get the below error:
undefined Index filename
Friend i wil give code that i have done for uploading image.This surely work.
You can refer this.Any doubt,please ask,i wil help you
public function uploadImage() {
$this->load->helper(array('form', 'url'));
$config['upload_path'] = 'assets/images/b2bcategory';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000';
$config['max_width'] = '2024';
$config['max_height'] = '1768';
$config['width'] = 75;
$config['height'] = 50;
if (isset($_FILES['catimage']['name'])) {
$filename = "-" . $_FILES['catimage']['name'];
$config['file_name'] = substr(md5(time()), 0, 28) . $filename;
}
$config['overwrite'] = TRUE;
$config['remove_spaces'] = TRUE;
$field_name = "catimage";
$this->load->library('upload', $config);
if ($this->input->post('selsub')) {
if (!$this->upload->do_upload('catimage')) {
//no file uploaded or failed upload
$error = array('error' => $this->upload->display_errors());
} else {
$dat = array('upload_data' => $this->upload->data());
$this->resize($dat['upload_data']['full_path'], $dat['upload_data']['file_name']);
}
$ip = $_SERVER['REMOTE_ADDR'];
if (empty($dat['upload_data']['file_name'])) {
$catimage = '';
} else {
$catimage = $dat['upload_data']['file_name'];
}
$data = array(
'ctg_image' => $catimage,
'ctg_dated' => time()
);
$this->b2bcategory_model->form_insert($data);
}
}
Your path is wrong.
'upload_path' => '' .base_url(). '/assets/img/logo/',
Note: Make sure your upload path "assets folder" is in the main directory out side of
application folder. You also may need to set folder permissions.
Try
'upload_path' => FCPATH . 'assets/img/logo/'
or just
'upload_path' => './assets/img/logo/'
To access file info after upload success preferences
$image_info = $this->upload->data();
echo $image_info['file_name'];
Or Through Model
$this->insert_model->form_insert($data, $image_info = array());
I am trying to check if the upload file field (<input type="file" name="userfile">) is empty or other validation on image upload field. In the view I echo <?php echo $upload_error;?> but it says $upload_error is Undefined variable. what is the problem with my code and how can i fix that.
This is my controller function
function add_main_products(){
$this->form_validation->set_rules('product_name', 'Main Product Category', 'required');
if ($this->form_validation->run() == FALSE){
$this->load->view('admin/admin_add_mainproduct');
}
else{
$config['upload_path'] = 'application/views/uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$this->load->library('upload', $config);
if (! $this->upload->do_upload('userfile')){
$error['upload_error'] = array('error' => $this->upload->display_errors());
$this->load->view('admin/admin_add_mainproduct', $error);
return FALSE;
}
$this->mod_products->add_main_product($this->upload->data());
$data = array('upload_data' => $this->upload->data());
$data['result']="Main Product Added Successfully";
$this->load->view('admin/admin_add_mainproduct', $data);
}
}
Not sure if I understand your question properly but you can set rules the same way for file input:
$this->form_validation->set_rules('userfile', 'Image', 'required');
Let me know if this helps