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);
}
}
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);
}
//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.
Actually, i'm trying to upload file in codigniter it's done but i want if file is already exist in uploads folder will show error message please help me.
Here my code controller:
if($this->input->post('userSubmit'))
{
//echo "comm";
if(!empty($_FILES['file_upload']['name']))
{
// echo "coming";
$config['upload_path'] = 'uploads/';
$config['allowed_types'] = 'doc|docx|pdf';
$config['file_name'] = $_FILES['file_upload']['name'];
$this->load->library('upload',$config);
$this->upload->initialize($config);
if($this->upload->do_upload('file_upload'))
{
//echo "cominggggggg";die;
$uploadData = $this->upload->data();
$picture = $uploadData['file_name'];
//print_r($picture);die;
}
else
{
$picture = '';
}
}
else
{
$picture = '';
}
}
You don't need to initialize the CI upload settings twice and you should use ./uploads/ rather than uploads/. Otherwise it is just a matter of checking like so:
if ($this->input->post('userSubmit')) {
if (!empty($_FILES['file_upload']['name'])) {
if (is_file('./uploads/' . $_FILES['file_upload']['name'])) {
show_error('File already exists!'); // function exits
}
$config['upload_path'] = './uploads/'; // correct way
$config['allowed_types'] = 'doc|docx|pdf';
$config['file_name'] = $_FILES['file_upload']['name'];
$this->load->library('upload', $config);
//$this->upload->initialize($config); you already did this ^
if ($this->upload->do_upload('file_upload')) {
$uploadData = $this->upload->data();
$picture = $uploadData['file_name'];
} else {
$picture = '';
}
} else {
$picture = '';
}
}
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