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
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 the multiple images through my form. I have taken care of many expect like form_open_multipart, calling a name as Array in upload input.
I have no issue with the images upload. I can upload multiple images successfully, but the problem comes when I select any text or non-image file with the images.
I am checking scenarios that if user mixes it up (accidently select any other file with images) images with text file then some files are getting uploaded and then I get the error of the wrong type of file.
In Ideal case, it may not allow uploading the file if any one file does not have the correct file type.
so can any one help me over that how can I check before file upload that all files are allowed type or not?
I tried many examples but get the same problem In all.
There is no problem if i upload the multiple images. It's work fine.
<input type="file" name="pic_url[]" multiple accept="image/*" />
And this below is my controller
$config['upload_path'] = './assets/prop_pic/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 20000;
$config['encrypt_name'] = TRUE;
$config['detect_mime'] = TRUE;
//initialize upload class
$this->load->library('upload', $config);
$upload_error = array();
for ($i = 0; $i < count($_FILES['pic_url']['name']); $i++) {
$_FILES['pic_url']['name'] = $_FILES['pic_url']['name'][$i];
$_FILES['pic_url']['type'] = $_FILES['pic_url']['type'][$i];
$_FILES['pic_url']['tmp_name'] = $_FILES['pic_url']['tmp_name'][$i];
$_FILES['pic_url']['error'] = $_FILES['pic_url']['error'][$i];
$_FILES['pic_url']['size'] = $_FILES['pic_url']['size'][$i];
if (!$this->upload->do_upload('pic_url')) {
// fail
$upload_error = array('error' => $this->upload->display_errors());
//$this->load->view('multiple_upload_view', $upload_error);
$this->session->set_flashdata('upload_error', $upload_error);
redirect('property_master/view_prop_master_form');
break;
}
}
First problem is primarily down to how you handle your errors.
if (!$this->upload->do_upload('pic_url')) {
// fail
$upload_error = array('error' => $this->upload->display_errors());
//$this->load->view('multiple_upload_view', $upload_error);
$this->session->set_flashdata('upload_error', $upload_error);
redirect('property_master/view_prop_master_form');
break;
}
The above code will simply halt the rest of the for loop and redirect on the first error.
A simple solution would be to change that to log failures and continue processing:
$config['upload_path'] = './assets/prop_pic/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 20000;
$config['encrypt_name'] = TRUE;
$config['detect_mime'] = TRUE;
//initialize upload class
$this->load->library('upload', $config);
$upload_error = array();
for ($i = 0; $i < count($_FILES['pic_url']['name']); $i++) {
$_FILES['pic_url']['name'] = $_FILES['pic_url']['name'][$i];
$_FILES['pic_url']['type'] = $_FILES['pic_url']['type'][$i];
$_FILES['pic_url']['tmp_name'] = $_FILES['pic_url']['tmp_name'][$i];
$_FILES['pic_url']['error'] = $_FILES['pic_url']['error'][$i];
$_FILES['pic_url']['size'] = $_FILES['pic_url']['size'][$i];
if (!$this->upload->do_upload('pic_url')) {
$upload_error[$i] = array('error' => $this->upload->display_errors());
}
}
if (!empty($upload_error)) {
$this->session->set_flashdata('upload_error', $upload_error);
redirect('property_master/view_prop_master_form');
}
Alternatively, you would need to look at manually validating files based on their file extension.
$config['upload_path'] = './assets/prop_pic/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 20000;
$config['encrypt_name'] = TRUE;
$config['detect_mime'] = TRUE;
//initialize upload class
$this->load->library('upload', $config);
$upload_error = array();
foreach ($_FILES['pic_url'] as $key => $file) {
$ext = pathinfo($file['name'], PATHINFO_EXTENSION);
if (strpos($config['allowed_types'], $ext) === false) {
$upload_error[$key] => array('error' => sprintf('<p>Invalid file extension (%s)</p>', $ext));
continue;
}
$_FILES['userfile'] = $file;
$this->upload->initialize($config);
if (!$this->upload->do_upload('userfile')) {
$upload_error[$key] = array('error' => $this->upload->display_errors());
} else {
// do something with $this->upload->data();
}
}
if (!empty($upload_error)) {
$this->session->set_flashdata('upload_error', $upload_error);
redirect('property_master/view_prop_master_form');
}
Hope that helps
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';
I want to resize uploaded image in codeigniter but it is not working with this code. Image uploading successfully but I want to resize it to 110x110 and display it !
my code is here
class Upload_photo extends CI_Controller{
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
}
public function index(){
session_start();
$u = $_SESSION['username'];
$config['upload_path'] = 'user/'.$u.'/'.$filename;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '30000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload',$config);
if(!$this->upload->do_upload('file1'))
{
echo "Error". $this->upload->display_errors();
}
else {
$config['image_library'] = 'GD2';
$config['source_image'] = $config['upload_path'];
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 110;
$config['height'] = 110;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
echo 'Photo Uploaded Successfully';
}
}
}
First you upload the original image so set the configs and upload the original image, then you resize the image. With that said:
$fullPath = 'user/' . $u . '/';
$config['upload_path'] = $fullPath;
$config['file_name'] = 'theNameofFile';
$config['allowed_types'] = 'jpg|png|bmp';
$config['max_size'] = '30000';
$config['overwrite'] = FALSE;
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if($this->upload->do_upload('file1') == FALSE)
{
echo "Error:" . $this->upload->display_errors();
return;
}
At this point the the file was uploaded. Now you need to grab it and resize.
// Reset
$config = array();
$config['image_library'] = 'gd2';
$config['source_image'] = $fullPath . $this->upload->data()['file_name'];
$config['create_thumb'] = FALSE;
$config['maintain_ratio'] = TRUE;
$config['width'] = 100;
$config['height'] = 100;
$this->load->library('image_lib', $config);
$this->image_lib->resize();
Finally, if you wish to, you can delete the original image.
If you want both images 1>uploded image 2>Thumbnail Image and want to store Thumbnail image to different folder you should use $config['new_image'] and path should not your url but should be absolute server path. If you don't know how to get absolute server path ? you can see my code below.
I got the solution, my code is here
class Upload_photo extends CI_Controller{
function __construct()
{
parent::__construct();
$this->load->helper(array('form', 'url'));
}
public function index(){
session_start();
$u = $_SESSION['username'];
$config['upload_path'] = 'user/'.$u;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '30000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload',$config);
$filename = $_FILES['file1']['name'];
//echo $filename;
//echo $source;
if(!$this->upload->do_upload('file1'))
{
echo "Error". $this->upload->display_errors();
return;
}
$config = array();
$config['image_library'] = 'gd2';
$config['source_image'] = 'user/'.$u.'/'.$filename ;
$config['new_image']= FCPATH . 'user/'.$u.'/Thumb/';
$config['create_thumb'] = TRUE;
$config['maintain_ratio'] = FALSE;
$config['width'] = 110;
$config['height'] = 110;
$this->load->library('image_lib',$config);
if(!$this->image_lib->resize())
{
echo $this->image_lib->display_errors();
}
}
}
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"