Images not saving to temporary folder only saved to Database - php

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);
}

Related

How to upload multiple image in few records in database using codeigniter?

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);
}

How to encrypt image name and save into database?

I can save the image name into database without any problem but when I try to encrypt the name only the image path gets the encrypted name but the database not
public function fileUpload(){
$clientes = $this->pm->getAllProducts();
foreach ($clientes as $cliente) {
$teste = $cliente->id;
}
if(!empty($_FILES['file']['name'])){
// Set preference
$config['upload_path'] = "uploads/";
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$config['max_size'] = '5024'; // max_size in kb
$config['encrypt_name'] = TRUE;
$config['file_name'] = $_FILES['file']['name'];
$this->upload->initialize($config);
//Load upload library
$this->load->library('upload',$config);
$data = array(
'pub_id' => $teste,
'url' => $_FILES['file']['name']
);
$this->pm->addProduct($data);
// File upload
if($this->upload->do_upload('file')){
// Get data about the file
$uploadData = $this->upload->data();
}
}
}
You can encrypt file name with config
$config['encrypt_name'] = TRUE;
Or
$file= uniqid().time().$_FILES["file"]['name'];
$config['file_name'] = $file;

File Upload in Codeigniter

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);
}
}

PDF File not uploading

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();
}

Codeigniter after renaming file for upload how do i save the new file name to my database

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'],
);

Categories