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);
}
}
Related
//my model test.php is as follows
public function saveallpars($id)
{
$config['upload_path'] = sys_get_temp_dir();
$config['allowed_types'] = 'txt';
$config['xss_clean'] = false;
$config['overwrite'] = true;
$config['return_temp_file'] = true;
$this->load->library('upload', $config);
if (!$file = $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
dd($error);
}
else
{
$contents = explode("\n", file_get_contents($file));
$this->data= serialize($contents);
$this->db->where(array('id' => $id))->set(array('data' =>$this->data))->update($this->_table);
}
I am trying to save the contents of the text file in the data base field "data". But it is not saved. Any help would be appreciated.
I have a form in CodeIgniter that allows the user to upload 2 separate files. At the backend I want these files to get stored in the different folder. In the controller i have written the upload code
public function upload()
{
/**Start uploading file**/
$config['upload_path'] = './assets/file/.';
$config['allowed_types'] = 'gif|jpg|png|doc|txt';
$config['max_size'] = 1024 * 8;
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
if (!$this->upload->do_upload('file'))
{
$error = array('error' => $this->upload->display_errors());
echo $error;
}
else
{
$data = $this->upload->data();
echo $file = $data['file_name']; //name of file
}
/**End uploading file**/
/**Start uploading img**/
$config2['upload_path'] = './assets/img/.';
$config2['allowed_types'] = 'gif|jpg|png|doc|txt';
$config2['max_size'] = 1024 * 8;
$config2['encrypt_name'] = TRUE;
$this->load->library('upload', $config2);
if (!$this->upload->do_upload('img'))
{
$error1 = array('error' => $this->upload->display_errors());
echo $error1;
}
else
{
$data1 = $this->upload->data();
echo $img = $data1['file_name']; //name of img
}
/**End uploading img**/
}
The images are getting uploaded but they are getting uplaoded to same folder. Can anyone please tell how i can make the files get saved in seperate folders
in the second load , you need to initialiaze the loaded library because the load method don't initialize it :
$this->upload->initialize($config2);
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 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();
}
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