I have a Input Field which have same name. pdf[]
I am inserting and uploading it into database it is perfectly inserting into database. but not uploading into uploads folder there is a problem with do_upload();
$items=$_FILES['pdf']['name'];
$count = count($items);
$lstid=$this->FetchData->getLastInserted();
for($i=0; $i<=$count-1; $i++){
//echo $img[$i];
$img = $_FILES['pdf']['name'][$i];
$timezone = new DateTimeZone('Asia/Calcutta');
$datetime = new DateTime("now", $timezone);
$date = $datetime->format("D_M_d_Y_H_i_s");
$ext = pathinfo($img, PATHINFO_EXTENSION);
$new_name=$i.'_'.$state_id.'_'.$date;
if(!empty($img)){
$imagenamefordatabase = $new_name.'.'.$ext;
}
else{
$imagenamefordatabase ='';
}
$config['upload_path'] = 'uploads/';
// set the filter image types
$config['allowed_types'] = 'pdf';
$config['file_name'] = $new_name;
//load the upload library
$this->load->library('upload', $config);
$this->upload->initialize($config);
$this->upload->set_allowed_types('*');
$data['upload_data'] = '';
//if not successful, set the error message
if (!$this->upload->do_upload('pdf[]')) {
$data = array('Success' => $this->upload->display_errors());
}
else {
//else, set the success message
$data = array('Success' => "Upload success!");
$data['upload_data'] = $this->upload->data();
}
$sampleresult = array(
'Report_Print_Data_Id' => $lstid,
'Sample_Report' => $imagenamefordatabase
);
$this->AddData->addReportResult($sampleresult);
}
Change The filed name As userfile.
config file size in controller and try.
if any error please show.
You have to pass field name only without large brackets []. Update the following part of your code.
if (!$this->upload->do_upload('pdf')) {
$data = array('Success' => $this->upload->display_errors());
}
else
{
//else, set the success message
$data = array('Success' => "Upload success!");
$data['upload_data'] = $this->upload->data();
}
Related
I want uploads to save to upload/images folder in my project directory but it is not. The upload/images folder is empty while the upload saves to database directory
public function addArticleForm(){
//check whether user upload picture
if(!empty($_FILES['image_path']['upload_path'])){
$config['upload_path'] = 'uploads/images/';
$config['allowed_types'] = 'jpg|jpeg|png|gif|';
$config['file_name'] = $_FILES['image_path']['upload_path'];
//load upload library and initialize configuration
$this->load->library('upload',$config);
$this->upload->initialize($config);
if($this->upload->do_upload('image_path')){
$uploadData = $this->upload->data();
$image_path = $uploadData['file_name'];
}else{
$image_path = '';
}
}
Try This for setting up a dynamic function for image upload
public function upload_images($path,$filename){
$config = array(
'upload_path' => $path,
'allowed_types' => "gif|jpg|png|jpeg",
'overwrite' => TRUE,
'max_size' => "204800", // 20 mb
);
$this->load->library('upload', $config);
$this->upload->initialize($config);
if($this->upload->do_upload($filename)){
$upload_data = $this->upload->data();
$datas = array('file_name' => $upload_data['file_name'], 'file_size'=>$upload_data['file_size'], 'file_type'=>$upload_data['file_type']);
$file_name = $upload_data['file_name'];
$file_size = $upload_data['file_size'];
$file_type = $upload_data['file_type'];
} else {
$datas = array('error' => $this->upload->display_errors());
$this->session->set_flashdata('error_msg',$datas['error']);
}
return $datas;
}
And use above function in another function, Like
public function Add_Customer_Profiles(){
if($this->session->userdata('userid')!="" || null!==$this->session->userdata('userid')){
$custno = $this->input->post('custno',TRUE);
$fname = $this->input->post('fname',TRUE);
$lname = $this->input->post('lname',TRUE);
$email = $this->input->post('email',TRUE);
$mobile = $this->input->post('mobile',TRUE);
$path = './assets/profile_data/';
$img_name = 'customer_img'; // Your file input name
$file_detail = $this->upload_images($path,$img_name);
if(!isset($file_detail['error'])){
//Your Code here
}
}
public function addArticleForm(){
$config['upload_path'] = "./backend/images/";
$config['allowed_types'] = 'gif|jpeg|png|jpg';
$this->path = './backend/images/';
$this->load->library('upload',$config);
$this->upload->initialize($config);
if (!$this->upload->do_upload('file_name'))
{
$error = array('error'=>$this->upload->display_errors());
}else{
$dataUpload = $this->upload->data();
//echo $dataUpload['file_name'];
}
$data = array(
'name'=>$this->input->post('name'),
'image'=>$dataUpload['file_name'],
'created_by'=>$this->input->post('created_by'));
$insertdata = $this->db->insert('table_name',$data);
}
how to upload image file and pdf data, I find it difficult because the extension in different I have made simplified but the data does not go into the folder in the show..
function add_module_external(){
$id_module = "9090909";
$file_data = array();
$image_data = array();
$config_image["file_name"] = $id_module;
$config_image["upload_path"] = "./assets/file/image-module/";
$config_image["overwrite"] = TRUE;
$config_image["allowed_types"] = "gif|jpg|png|";
$this->load->library('upload', $config_image);
$this->upload->initialize($config_image);
$upload_background = $this->upload->do_upload('other_image');
if($upload_background){
$image_data = $this->upload->data();
$image = $image_data['file_name'];
}else{
$image = $this->input->post('background');
}
// pdf and ppt upload
$config_file['file_name'] = $id_module;
$config_file['upload_path'] = '/assets/file/materi-module/';
$config_file['overwrite'] = TRUE;
$config_file['allowed_types'] = 'pdf';
$this->load->library('upload', $config_file);
$this->upload->initialize($config_file);
$upload_pdf = $this->upload->do_upload('pdf_data');
if($upload_pdf){
$file_data = $this->upload->data();
$content = $file_data["file_name"];
}else{
$content = $this->input->post("text_data");
}
$query = $this->my_module->new_module($image, $id_module, $content);
redirect($this->agent->referrer());
}
On the second time you are loading the upload library, it will be skipped because you already loaded the library, so the file config will still being loaded on the image upload.
What you could do :
1. Load the library using provided image config
2. Process image upload data
3. Initialize upload using file config
4. Process file upload data
function add_module_external(){
$id_module = "9090909";
$file_data = array();
$image_data = array();
$config_image["file_name"] = $id_module;
$config_image["upload_path"] = "./assets/file/image-module/";
$config_image["overwrite"] = TRUE;
$config_image["allowed_types"] = "gif|jpg|png|";
$this->load->library('upload', $config_image);
$upload_background = $this->upload->do_upload('other_image');
if($upload_background){
$image_data = $this->upload->data();
$image = $image_data['file_name'];
}else{
$image = $this->input->post('background');
}
// pdf and ppt upload
$config_file['file_name'] = $id_module;
$config_file['upload_path'] = '/assets/file/materi-module/';
$config_file['overwrite'] = TRUE;
$config_file['allowed_types'] = 'pdf';
$this->upload->initialize($config_file);
$upload_pdf = $this->upload->do_upload('pdf_data');
if($upload_pdf){
$file_data = $this->upload->data();
$content = $file_data["file_name"];
}else{
$content = $this->input->post("text_data");
}
$query = $this->my_module->new_module($image, $id_module, $content);
redirect($this->agent->referrer());
}
You can upload jpg and pdf files together, but you can't rename the file name
Try this code.
if(!empty($_FILES["ktp"])) {
if(!file_exists("./uploads/".$this->input->post('no_hp'))) {
mkdir("./uploads/".$this->input->post('no_hp'));
}
$configktp['upload_path'] = './uploads/'.$this->input->post('no_hp')."/";
$configktp['allowed_types'] = '*';
$this->load->library('upload', $configktp);
if ( ! $this->upload->do_upload('ktp'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
}
if(!empty($_FILES["ijazah"])) {
$configijazah['upload_path'] = './uploads/'.$this->input->post('no_hp')."/";
$configijazah['allowed_types'] = '*';
$this->load->library('upload', $configijazah);
if ( ! $this->upload->do_upload('ijazah'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
}
if(!empty($_FILES["cv"])) {
$configpdf['upload_path'] = './uploads/'.$this->input->post('no_hp')."/";
$configpdf['allowed_types'] = '*';
$this->load->library('upload', $configpdf);
if ( ! $this->upload->do_upload('cv'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
}
I am trying to upload image using code-igniter, but it's not possible, why I don't know. Can anyone help me?
Here is code I have tried
public function upload(){
$config['upload_path'] = "./assets/uploads/";
$config['allowed_types'] = "jpg|png|jpeg|gif";
$this->load->library("upload" , $config);
$rs = $this->upload->do_upload("userfile");
print_r($rs);
print_r($_FILES);
if(!$rs){
$error = array('error'=>$this->upload->display_errors());
$this->load->view("main_view", $error);
}
else{
$file_data = $this->upload->data(); $data['img'] = "localhost/FileUpload/assets/uploads/";
$file_data['file_name']; $this->load->view("success_msg" , $data);
}
}
I'm not able to actually test this of course, but I believe this will get you started.
public function upload() {
$name = 'something'; // set this based on your requirements, I often use random strings or user names according to the situation.
$config['upload_path'] = "assets/uploads/";
$config['allowed_types'] = "jpg|png|jpeg|gif";
$config['file_name'] = $name;
$this->load->library("upload" , $config);
$rs = $this->upload->do_upload("userfile");
print_r($rs);
print_r($_FILES);
if(!$rs) {
$error = array( 'error'=>$this->upload->display_errors() );
$this->load->view("main_view", $error);
}
else {
$file_data = $this->upload->data();
$file_name = $file_data['file_name'];
$ext = pathinfo($file_name, PATHINFO_EXTENSION); //Useful to store your name in database if/when you need to
$data = array('upload_data' => $upload_data);
$this->load->view("success_msg" , $data);
}
}
I want to upload PDF file as well as image file with on one do_upload
I want to upload two different files in two different directory by using codeigniter. I wrote the following code in my model. but it will upload only the first image.
if($_POST){
if($_FILES['productimage']['name']){
$img = $_FILES['productimage']['name'];
$config['upload_path'] = './uploads/products/';
$config['allowed_types'] = 'png|jpg|gif|bmp';
$config['overwrite'] = TRUE;
$this->load->library('upload',$config);
if(!$this->upload->do_upload('productimage'))
{
$errors = array('error' => $this->upload->display_errors());
$img="";
}
else
{
$data =$this->upload->data();
$img=$data['file_name'];
//print_r($img);die;
}
}else{
$img=$this->input->post('image_old');
}
if($_FILES['productpdf']['name']){
$img = $_FILES['productpdf']['name'];
$config['upload_path'] = './uploads/products/';
$config['allowed_types'] = 'png|jpg|gif|bmp|pdf';
$config['overwrite'] = TRUE;
$this->load->library('upload',$config);
if(!$this->upload->do_upload('productpdf'))
{
$errors = array('error' => $this->upload->display_errors());
$pdf="";
}
else
{
$data =$this->upload->data();
$pdf=$data['file_name'];
}
}else{
$pdf=$this->input->post('pdf_old');
}
// print_r($img);print_r($pdf);die;
$title = $this->input->post('productname');
$content = $this->input->post('description');
$status = $this->input->post('status');
$this->db->where('product_id', $id);
$this->db->update('products',array('product_name'=>$title,'product_content'=>$content,'product_image'=>$img,'product_file'=>$pdf,'product_status'=>$status));
$this->db->where('product_id',$id);
$this->db->delete('products_filter');
$filters= $_POST['filter'];
foreach ($filters as $value)
{
$this->db->insert('products_filter',array('product_id' => $id,'products_search_id'=>$value));
}
return ($this->db->affected_rows() != 1)? false:true;
}else{
redirect(base_url('admin/products/product-list/'.$redirectid));
}
}
Please check my code
this code to handle pdf or images file upload in different directory
<?php
if($_POST){
if($_FILES['productimage']['name'])
{
$custom_file_name = $_FILES['productimage']['name'];
$file_ext = end(explode('.', $custom_file_name));
if($file_ext=='pdf')
{
$upload_path_directory='./uploads/products/pdf/';
$file_field_name="productimage";
}
else{
$upload_path_directory='./uploads/products/images/';
$file_field_name="productpdf";
}
$config['upload_path'] = $upload_path_directory;
$config['allowed_types'] = 'png|jpg|gif|bmp|pdf';
$config['overwrite'] = TRUE;
$this->load->library('upload',$config);
if(!$this->upload->do_upload("$file_field_name"))
{
$errors = array('error' => $this->upload->display_errors());
$custom_file_name="";
}
else
{
$data =$this->upload->data();
$custom_file_name=$data['file_name'];
}
?>
First to make sure to upload library libraries and form validation if don't know where to set libaray will mention below*
$this->load->libaray("upload");
$this->load->libaray("form_validation");
to add this code in if condition
I have an issue with codeigniter file upload library. I am trying to upload multiple files (7 images), i renamed them and after which uploaded to my server file system. But i cannot get the new name to save in my database.
Here is my Controller
if (isset($_POST['carform']))
{
$dir = './uploads/cars';
$config['upload_path'] = $dir;
$config['allowed_types'] = 'jpg|jpeg|png';
$config['max_size'] = '2048';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['remove_spaces'] = 'TRUE';
$uid = $this->tank_auth->get_user_id();
$config['file_name'] = time().$uid;
$this->load->library('upload', $config);
foreach($_FILES as $field => $file)
{
if($file['error'] == 0)
{
// So lets upload
if ($this->upload->do_upload($field))
{
$data = array('upload' => $this->upload->data());
$this->cars_model->set_car($data);//end foreach loop.....
$id = $this->db->insert_id();
redirect('/cars/details/'.$id.'');
}else
{
$errors = $this->upload->display_errors();
echo($errors);
}
}
}
}//end if statement ...
You do have a helper function within Codeigniter File Uploading Class: $this->upload->data(). In order to get the filename do this:
$my_data=$this->upload->data();
$file_name=$my_data['filename'];
check the manual here
edit:
to insert data into database assuming $filename is a string you need first explode it into an array
$filename="14010989464.jpg 140109894641.jpg 140109894642.jpg 140109894643.jpg 140109894644.jpg 140109894645.jpg 140109894646.jpg";
$fn=explode(' ',$filename);
then you can insert the data (imagename) into the column img of your table test like this:
foreach ($fn as $key=>$val){
$data['img']=$fn[$key];
$this->db->insert('test', $data);
}
can use for
ex :
$this->load->library('upload', $config);
for($i=1;$i<=5;$i++) //5 = total image
{
if (!$this->upload->do_upload('userfile'.$i))
{
$data['error'] = array('error' => $this->upload->display_errors());
}
else
{
$upload_data = $this->upload->data();
$data[$i] = $upload_data['file_name'];
}
}
$data_x = array(
'image1' => $data['1'],
'image2' => $data['2'],
'image3' => $data['3'],
'image4' => $data['4'],
'image5' => $data['5'],
);