I am Trying to Upload two file with from Same form with Different File name.
The First File is always Uploading But the Other one never Uploading.
code for Model is:
public function add_imgup()
{
$this->uppdf();
$this->upphoto();
}
public function upphoto()
{
$upload = array();
$current_timestamp = time();
$image_name = $current_timestamp.'_image';
$config['upload_path'] = $this->config->item('file_upload_absolute_path')."pdf/";
$config['allowed_types'] = 'pdf|PDF|DOC|doc|docx';
$config['overwrite'] = true;
$config['file_name'] = $image_name;
$this->load->library('upload',$config);
$file_image = $this->upload->do_upload('ccv');
$image = array('upload_data' => $this->upload->data());
}
public function uppdf()
{
$upload = array();
$current_timestamp = time();
$image_name = $current_timestamp.'_image';
$config['upload_path'] = $this->config->item('file_upload_absolute_path')."photo/";
$config['allowed_types'] = 'jpg|jpeg|png|gif|bmp';
$config['overwrite'] = true;
$config['file_name'] = $image_name;
$this->load->library('upload',$config);
$file_image = $this->upload->do_upload('iimg');
$image = array('upload_data' => $this->upload->data());
}
Check the print_r($_FILE) after submitting the form to confirm the you submitted correct form, also check your form is enctype="multipart/form-data"
Related
In my page I want user to select multiple images and upload it I am saving images name in database for reference. I am successful in uploading single images in database and can also show image in view but now I have problem in uploading multiple images.
public function add_record()
{
$this->form_validation->set_rules('category', 'category', 'required');
$current_date = date("Y-m-d H:i:s");
$error='';
if($this->form_validation->run())
{
$image = '';
if($_FILES['image']['name'])
{
if (!is_dir('/backend_assets/media/image/')) {
mkdir('./backend_assets/media/image/', 0777, TRUE);
}
$config['upload_path'] = './backend_assets/media/image/';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ($this->upload->do_upload('image'))
{
$data = $this->upload->data();
$image = $data['file_name'];
}else{
$this->session->set_flashdata('error', $this->upload->display_errors());
redirect(base_url('admin/image'));
}
}
$insert_array = array(
'gl_cat_id' => $this->input->post('category'),
'gl_image'=> $image
);
if ($this->common_model->add_records('vm_image',$insert_array))
{
$id = $this->db->insert_id();
$insert_sco_details = array(
'sd_ty'=>'vm_image',
'sd_ty_id'=>$id,
'sd_image'=>$image
);
if($this->common_model->publication('vm_image',$id) && $this->common_model->add_records('vm_seo_detail',$insert_sco_details))
{
$this->session->set_flashdata('success','Record added successfully');
redirect(base_url('admin/image'));
}else{
$this->session->set_flashdata('error','Error while adding record');
redirect(base_url('admin/image'));
}
}else{
$this->session->set_flashdata('error','Error while adding record');
redirect(base_url('admin/image'));
}
}
$where_array = array('vm_publications.status !=' => 4);
$data['users_type'] = $this->common_model->get_records('vm_image_category','','','');
$data['include'] = 'backend/image/add_image';
$this->load->view('backend/container', $data);
}
How is it possible with above code...?
$current_date = date("Y-m-d H:i:s");
$error = '';
$image = '';
if(isset($_FILES['image']['name']))
{
//print_r($_FILES);
$id = base64_decode($this->input->post('gid'));
$filesCount = count($_FILES['image']['name']);
$inserted = '';
for($i = 0; $i < $filesCount; $i++)
{
$_FILES['userFile']['name'] = $_FILES['image']['name'][$i];
$_FILES['userFile']['type'] = $_FILES['image']['type'][$i];
$_FILES['userFile']['tmp_name'] = $_FILES['image']['tmp_name'][$i];
$_FILES['userFile']['error'] = $_FILES['image']['error'][$i];
$_FILES['userFile']['size'] = $_FILES['image']['size'][$i];
$config['upload_path'] = './backend_assets/media/image/';
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if($this->upload->do_upload('userFile'))
{
$fileData = $this->upload->data();
$image = $fileData['file_name'];
$insert_array = array(
'gl_cat_id' => $this->input->post('category'),
'gl_image'=> $image
);
if ($this->common_model->add_records('vm_image',$insert_array))
{
$id = $this->db->insert_id();
$insert_sco_details = array(
'sd_ty'=>'vm_image',
'sd_ty_id'=>$id,
'sd_image'=>$image
);
if($this->common_model->publication('vm_image',$id) && $this->common_model->add_records('vm_seo_detail',$insert_sco_details))
{
$inserted++;
}
}
}
}
if($inserted == $filesCount)
{ $this->session->set_flashdata('success','Images uploaded successfully');
redirect(base_url('adminp8AamG6ueHFNGAAp/image'));
}else{
$this->session->set_flashdata('error','Error while adding record');
redirect(base_url('adminp8AamG6ueHFNGAAp/image'));
}
}
$where_array = array('vm_publications.status !=' => 4);
$data['users_type'] = $this->common_model->get_records('vm_image_category','','','');
$data['include'] = 'backend/image/add_image';
$this->load->view('backend/container', $data);
try below code in you add_record() function. it will helpful to you. few days ago, i have faced same problem
$files = $_FILES;
$count = count($_FILES['image']['name']);
for($i=0; $i<$count; $i++) {
$_FILES['image']['name']= $files['image']['name'][$i];
$_FILES['image']['type']= $files['image']['type'][$i];
$_FILES['image']['tmp_name']= $files['image']['tmp_name'][$i];
$_FILES['image']['error']= $files['image']['error'][$i];
$_FILES['image']['size']= $files['image']['size'][$i];
$this->upload->initialize($this->set_upload_options());//function defination below
$this->upload->do_upload('image');
$upload_data = $this->upload->data();
$name_array[] = $upload_data['file_name'];
$fileName = $upload_data['file_name'];
$images[] = $fileName;
}
$fileName = $images;
and set file upload configuration in set_upload_options function in same controller.
function set_upload_options() {
$config = array();
$config['upload_path'] = PATH;
$config['remove_spaces']=TRUE;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '78000';
return $config;
}
First, debug the output of the $_FILES variable. This should give you an array of files that are being uploaded. Loop through them to treat each one individually.
foreach ($_FILES as $file) {
// do some file processing on the $file object instead of $_FILES object.
// example: instead of using 'if($_FILES['image']['name'])'
// use: 'if($file['image']['name'])'
}
If you want to use CI's upload class, check out their Docs here: https://www.codeigniter.com/userguide3/libraries/file_uploading.html
I am trying to upload an image in root folder and its file name in database. here is what I did for the upload function:
public function add_blog($id=0){
if(!empty($_FILES['picture']['name'])){
$config['upload_path'] = 'uploads/blog_image';
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['file_name'] = $_FILES['picture']['name'];
//Load upload library and initialize configuration
$this->load->library('upload',$config);
$this->upload->initialize($config);
// print_r($value['name'][$s]);exit;
if($this->upload->do_upload('picture')){
$uploadData = $this->upload->data();
$picture = $uploadData['file_name'];
print_r($picture); exit;
}
}
print_r($config['file_name']); exit;
$data['blog_data']=array('blog_post'=>$this->input->post('blog_post'),
'posted_by'=>$this->input->post('posted_by'),
'blog_image'=>$picture);
if ($id==0){
$this->db->insert('blog',$data['blog_data']);
// $last_id = $this->db->insert_id();
}
else {
$this->db->where('id',$id);
// $last_id = $this->db->insert_id();
$this->db->update('blog',$data['blog_data']);
}
}
problem here is i am being able to insert other data except image. I get the image name with that print_r($config[file_name]) if i do print_r() and exit, if not it will just insert other data except image. But the image is neither uploaded in root folder nor its name in database. If I give the non existing upload path, then also its not throwing any error. I think code inside If is not executed. How can i solve this ? Thanks in advance.
private function _upload_image( ) {
$this->load->library( 'upload' );
if ($_FILES && $_FILES['picture']['name'] !== ""){
$config['upload_path'] = 'uploads/blog_image';
$config['allowed_types'] = 'jpg|jpeg|png|bmp';
$config['max_size'] = 10000;
/*the picture name must be unique, use function now()*/
$config['file_name'] = $_FILES['picture']['name'] . now();
$config['file_ext_tolower'] = TRUE;
$this->upload->initialize( $config );
if ( $this->upload->do_upload( 'picture' ) ){
$file_name = $this->upload->data()['file_name'];
$full_path = $this->upload->data()['full_path'];
/*If you want create a thumb, use this part*/
$this->load->library('image_lib');
$config = array(
'source_image' => $path,
'new_image' => $this->_image_path,
'maintain_ratio' => true,
'width' => 128,
'height' => 128,
'create_thumb' => TRUE,
'thumb_marker' => '_thumb',
);
$this->image_lib->initialize( $config );
$this->image_lib->resize();
/*Save in database*/
$this->db->insert('blog', [
'file_name' => $file_name,
'full_path' => $full_path
]);
} else {
//if picture is empty, do something
}
}
}
You do not need to use $_FILES && $_FILES ['picture']['name']! == "" only if your form has the picture field as an optional field, $this->upload->do_upload('picture') and get data from $this->upload->data(), read the manual
public function add_blog()
{
$config['upload_path'] = '.uploads/blog_image';
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['max_size'] = 10000;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('picture'))
{//Do something with errors
$errors = $this->upload->display_errors();
}
else
{
$data = $this->upload->data();
$this->db->insert('blog', [
'file_name' => $data['file_name'],
'full_path' => $data['full_path']
]);
}
}
I just didn't mention the file size to be uploaded. I did this in my above code and worked.
EDIT
public function add_blog($id=0){
if(!empty($_FILES['picture']['name'])){
$config['upload_path'] = 'uploads/blog_image';
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['max_size'] = 0;
$config['file_name'] = $_FILES['picture']['name'];
//Load upload library and initialize configuration
$this->load->library('upload',$config);
$this->upload->initialize($config);
// print_r($value['name'][$s]);exit;
if($this->upload->do_upload('picture')){
$uploadData = $this->upload->data();
$picture = $uploadData['file_name'];
// print_r($picture); exit;
}
}
// print_r($config['file_name']); exit;
$data['blog_data']=array('blog_post'=>$this->input->post('blog_post'),
'posted_by'=>$this->input->post('posted_by'),
'blog_image'=>$picture);
if ($id==0){
$this->db->insert('blog',$data['blog_data']);
// $last_id = $this->db->insert_id();
}
else {
$this->db->where('id',$id);
// $last_id = $this->db->insert_id();
$this->db->update('blog',$data['blog_data']);
}
}
And this code works for both insert and update.
Here my controller file this working fine except in thumbs folder name get changed to converted_thumb
example:
my original image name is converted.jpg but in thumbs folder it save as converted_thumb so i want to remove _thumb from image name please solve this issue
public function do_upload() {
$config['upload_path'] = './uploads'; // uploaded file store here
$config['allowed_types'] = 'jpg|png|jpeg|gif';
$config['max_size'] = ' 2097152';
$this->load->library('upload', $config);
if ($this->upload->do_upload()) {
$data = $this->upload->data();
//create copy of image
$configs['image_library'] = 'gd2';
$configs['source_image'] = $data['full_path'];
$configs['new_image'] = 'uploads/thumbs/'; //resize image will save here
$configs['create_thumb'] = 'false';
$configs['width'] = '250';
$configs['height'] = '250';
$this->load->library('image_lib', $configs);
$this->image_lib->resize();
$image_name = $data['file_name'];
//$full_path = $data['full_path'];
$post = array(
'product_name' => $image_name,
'product_path' => $configs['new_image'].$image_name
);
$this->db->insert('project', $post);
} else {
echo $this->upload->display_errors();
}
}
}
Why wouldn't you like the _thumbs from that? It is what it is for. Anyway do this.
rename("<?php echo base_url()?>/uploads/thumbs/YOUR_FILE_NAME_THUMBS", "<?php echo base_url()?>/uploads/thumbs/YOUR_FILE_NAME");
Try thumb_marker in your $configs array.
$configs['image_library'] = 'gd2';
$configs['source_image'] = $data['full_path'];
$configs['new_image'] = 'uploads/thumbs/'; //resize image will save here
$configs['create_thumb'] = 'false';
$configs['thumb_marker'] = ''; //Add this in your config array empty string
Then '_thumb' will not add in your newly created files. Source for more detail.
Just add thumb_marker to your config array and set it to FALSE
$configs['image_library'] = 'gd2';
$configs['source_image'] = $data['full_path'];
$configs['new_image'] = 'uploads/thumbs/'; //resize image will save here
$configs['create_thumb'] = 'false';
$configs['thumb_marker'] = FALSE; //this will remove the "_thumb" to your thumb image name
$configs['width'] = '250';
$configs['height'] = '250';
on this way i can upload image and pdf..
but i want to download the pdf or image from my view...please someone give me the code of downloading pdf or image from view..give me the full code
public function save_about_1() {
$about_1_image_info = $this->w_model->select_about_1_image();
$image_path = explode(base_url(), $about_1_info->about_1_link, 2);
unlink($image_path[1]);
$this->sa_model->delete_about_1($about_1_info->about_1_id);
$data = array();
/* Uplod start */
$config['upload_path'] = 'images/about_1/';
$config['allowed_types'] = 'gif|jpg|png|pdf|doc|xml';
$config[ 'overwrite'] = TRUE;
$config['max_size'] = '10000kb';
$config['max_width'] = '100240';
$config['max_height'] = '76800';
$error = array();
$fdata = array();
$this->load->library('upload', $config);
if (!$this->upload->do_upload('about_1_link')) {
$error = $this->upload->display_errors();
$edata = array();
$edata['error_message'] = $error;
$this->session->set_userdata($edata);
redirect('super_admin/about_1');
} else {
$fdata = $this->upload->data();
$data['about_1_link'] = base_url() . $config['upload_path'] . $fdata['file_name'];
$this->sa_model->save_about_1_info($data);
$sdata = array();
$sdata['message'] = "Saved Image Successfully";
$this->session->set_userdata($sdata);
redirect('super_admin/about_1');
}
}
You can use the CI download helper for this.
$data = file_get_contents("/path/to/photo.jpg"); // Read the file's contents
$name = 'myphoto.jpg';
force_download($name, $data);
From the CI User Guide.
https://ellislab.com/codeigniter/user-guide/helpers/download_helper.html
my csv file is locally uploading perfectly but when i upload it on my website then its not uploading following is my upload function
function upload_title()
{
if(isset($_POST['upload_titles'])) // check if submit button is clicked
{
$config['upload_path'] = 'uploads/title/';
$config['allowed_types'] = 'csv';
$config['max_size'] = '5000';
$config['overwrite'] = TRUE;
$config['encrypt_name'] = TRUE;
$config['remove_spaces'] = TRUE;
$this->load->library('upload', $config);
$this->upload->initialize($config);
$this->upload->do_upload();
$temp = $this->upload->data('userfile');
$article_titlelist = $temp['file_name'];
if($this->upload->do_upload())
{
$this->load->library('csvreader');
$result = $this->csvreader->parse_file('./uploads/title/'.$article_titlelist);
foreach($result as $val)
{
$articletitle = $val['title'];
$title_category = $val['category'];
$mDate = date('Y-m-d H:i:s');
$data = array(
'article_title' => $articletitle,
'title_category' => $title_category,
'article_cdate' => $mDate,
'article_mdate' => $mDate
);
$this->article_model->addtitle($data);
}
redirect('admin/article/title_listing');
}
}
$error = array('error' => $this->upload->display_errors());
$data['page_title'] = 'Article Title';
$data['content'] = $this->load->view('admin/add_title', $error, true);
$this->load->view('admin/template', $data);
}
i have check it online its not going in do_upload condition