Get file path of file uploaded using dropzone - codeigniter

How to get the file path of a file uploaded using dropzone in CodeIgniter?
My View page is
<form action="<?php echo site_url('Upload/imageUploadPost');?>" class="dropzone" id="my-awesome-dropzone" enctype="multipart/form-data" method="post">
<div class="fallback">
<input name="userfile" type="file"/>
</div>
</form>
My Controller is
public function imageUploadPost()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 1024;
$this->load->library('upload', $config);
$this->upload->do_upload('file');
//here I need to fetch the uploaded file details like name ,path
}

Hope this will help you :
Use $this->upload->data(); after upload successful to get the file details, Your method should be like this ::
public function imageUploadPost()
{
//print_r($_FILES);die;
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 1024;
$this->load->library('upload', $config);
if ($this->upload->do_upload('userfile'))
{
$data = array('upload_data' => $this->upload->data());
/* for full file path use $data['full_path'] and so like this */
$file_path = $data['full_path'];
echo $file_path ;
var_dump($data);
die();
}
else
{
$error = array('error' => $this->upload->display_errors());
var_dump($error);
die();
}
}
For more : https://www.codeigniter.com/user_guide/libraries/file_uploading.html

Related

Upload Image Using Path Codeigniter

I am facing problem in my uploading file using codeigniter.When I create a leave request and upload the image, the image did not appear in the folder path and also did not have a path at the database. Actually, I am doing Leave Request Management I already insert coding upload in my controller. This is my controller leaves.php
Controller (leaves.php)
public function upload ()
{
$config['upload_path'] = "./upload/";
$config['allowed_types'] = 'gif|jpg|png|jpeg';
//$config['max_size'] = 100;
//$config['max_width'] = 1024;
//$config['max_height'] = 768;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('create', $error);
}
else
{
$file_data = $this->upload->data();
$data['img'] = base_url().'/uploads/'.$file_data['file_name'];
$this->load->view('view', $data);
}
This is my view. Actually I has 2 view that called create.php and view.php.
Create.php is request leave and also for uploading file and for view.php is when user want to view their leave request and also the uploading image. This is my code for both view.
View(create.php)
<? echo form_open_multipart('create/upload');?>
<input type="file" name="image" class=" inputfile" value="<?php echo set_value ('image'); ?>" />
View(view.php)
<label for="image"><?php echo lang ('leaves_view_field_image'); ?>Uploaded File</label>
<img src="?php echo $leave['image']"><?php echo $leave['image']; ?>
When I upload the image with the leave request, the image uploading only fetch name file not the whole file in the database. Can someone help me ?
public function upload ()
{
$config['upload_path'] = "./upload/";
$config['allowed_types'] = 'gif|jpg|png|jpeg';
//$config['max_size'] = 100;
//$config['max_width'] = 1024;
//$config['max_height'] = 768;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('image'))
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('create', $error);
}
else
{
$file_data = $this->upload->data();
$data['img'] = base_url().'/uploads/'.$file_data['file_name'];
$this->load->view('view', $data);
}
}
Note: make sure folder read/write permission give in linux system
Here is the error if ( ! $this->upload->do_upload()) change it to if ( ! $this->upload->do_upload('image')) where "image" is the same thing as "$_FILES['image']" that is the val in your html e.g <input type="file" name="image">.
I hope this will help you to upload you file.

how to get upload file name in codeigniter

Hi i'm trying to upload image to a folder and image name in database
in view
echo form_open_multipart('subscribers_c/actioncreate',array('class'=>'form'));`enter code here`
<input type='file' name='img'>
in controller
$config['upload_path'] ='./assets/uploads/subscribers_photos'; //The path where the image will be save
$config['allowed_types'] = 'gif|jpg|png'; //Images extensions accepted
$config['max_size'] = '2048';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['overwrite'] = TRUE;
$this->load->library('upload', $config); //Load the upload CI library
if (!$this->upload->do_upload('img'))
{
$uploadError = array('upload_error' => $this->upload->display_errors());
//$this->set_flashdata('msg_error', $uploadError, site_url().'/subscribers_c/actioncreate');
}
$file_info = $this->upload->data('img');
$img_name = $file_info['file_name'];
$data=array(
'chit_fund_id'=>$this->input->post('cid'),
'first_name'=>$this->input->post('fname'),
'last_name'=>$this->input->post('lname'),
'dob'=>$this->input->post('bd'),
'gender'=>$this->input->post('g'),
'contact_number'=>$this->input->post('mob'),
'address'=>$this->input->post('add'),
'email_id'=>$this->input->post('eml'),
'bid_status'=>$this->input->post('b_status'),
'user_status'=>$this->input->post('u_status'),
'image_name'=>$img_name,
);
//print_r($data);
image_name is not having any value
how to get image name .
Instead of img in upload data
$file_info = $this->upload->data('img');
Try
$file_info = $this->upload->data();
$img = $file_info['file_name'];
And on here $config['upload_path'] = './assets/uploads/subscribers_photos'; end with /
Like $config['upload_path'] = './assets/uploads/subscribers_photos/';
Inside the view call the form_upload function(if needed, your one is also correct):
<?php echo form_upload('pic'); ?>
Inside controller function for upload:
$img_name = $_FILES["pic"]["name"];
By using this code you will get the file name.
$img_name = $this->upload->data('file_name');

Codeigniter Modular HMVC extension - cant upload file

I have a problem with uploading files by form. Everytime I try, I get the error "File type not allowed". I am using HMVC modular extension and my controllers extends MX_Controller.
I already tried to $var_dump($_FILES) from my form-view and the result is fine, but when I try to use method do_upload() in controller it doesnt work.
Does anyone have any idea? Here is the controller:
Class Store_data extends MX_Controller
{
function __construct()
{
parent::__construct();
}
function upload_pic()
{
//$data['item_id'] = $item_id;
$template = "bootstrap_theme";
//$current_url = current_url();
//$data['form_location'] = $current_url;
$data['view_file'] = "upload_pic";
$this->load->module('template');
$this->template->$template($data);
}
function do_upload()
{
//var_dump($_FILES);
$config['upload_path'] = './images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '10000';
$config['max_width'] = '4024';
$config['max_height'] = '4768';
$this->load->library('upload', $config);
$this->upload->overwrite = true;
$this->upload->initialize($config);
if ( ! $this->upload->do_upload())
{
$data['error'] = $this->upload->display_errors();
$template = "bootstrap_theme";
$data['view_file'] = "upload_pic";
$this->load->module('template');
$this->template->$template($data);
}
else
{
$data['upload_data'] = $this->upload->data();
$template = "bootstrap_theme";
$data['view_file'] = "upload_success";
$this->load->module('template');
$this->template->$template($data);
}
}
And here is the upload form view:
<?php echo $error;?>
<?php echo form_open_multipart('store_data/do_upload');?>
<input type="file" name="userfile" size="20" />
<br /><br />
<input type="submit" value="upload" />
</form>
I solved the problem.
I had to uncomment extension=php_fileinfo.dll in php.ini and comment ;extension=php_mime_magic.dll
Thanks anyway

CodeIgniter - Write file names to database from multiple images

I have an upload form with two file upload fields. I am trying to upload the files, and then write the file names to the same row in a database. I have everything working except the file names get written to their own row. I am guessing it's because I am making two separate calls to the model, but I am not sure how to go about doing it in one call since they each have their own "if" statement.
To make my question clear. How do I write the names of two uploaded files to the same row in the database?
Any ideas would be great. This is the code I am working with. Thanks.
View:
<?php echo form_open_multipart('upload_controller'); ?>
<p>
<input type="file" name="userfile">
</p>
<p>
<input type="file" name="userfile1">
</p>
<p><?php echo form_submit('submit', 'Upload them files!') ?></p>
<?php form_close() ?>
Controller:
if (isset($_POST['submit'])) {
$this->load->library('upload');
if (!empty($_FILES['userfile']['name'])) {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->upload->initialize($config);
// Upload file 1
if ($this->upload->do_upload('userfile')) {
$img = $this->upload->data();
$file_name = $img['file_name'];
$this->load->model('upload_file', 'upload_model');
$this->upload_model->upload_file1($file_name);
}
}
if (!empty($_FILES['userfile1']['name'])) {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->upload->initialize($config);
// Upload file 2
if ($this->upload->do_upload('userfile1')) {
$img = $this->upload->data();
$file_name = $img['file_name'];
$this->load->model('upload_file', 'upload_model');
$this->upload_model->upload_file2($file_name);
} else {
echo $this->upload->display_errors();
}
}
} else {
$this->load->view("upload_form");
}
Model:
public function upload_file1($file_name) {
$data = array('file1' => $file_name);
$this->db->insert('images_table', $data);
}
public function upload_file2($file_name) {
$data = array('file2' => $file_name);
$this->db->insert('images_table', $data);
}
Database row:
| id | file1 | file2 |
Try like this(if you have the number of files....in your case it is "2" )
$this->load->model('upload_file', 'upload_model');
for($i=1;$i<=$numimg;$i++) //in your case $numimg = 2;
{
if($this->upload->do_upload('File'.$i)) //File1,File2 are filenames
{
$img_data = $this->upload->data();
$images[$i] = $img_data['file_name'];
}
}
$data['Image'] = implode(',',$images);
$this->upload_model->upload_file($data);
at your model:
public function upload_file2($data)
{
$data = array('file_name' => $data['Image']);
$this->db->insert('images_table', $data);
}
That's it.If you want to retrive it from database use
$images = explode(',',$data->file_name);
it will give you an array of image names.

image upload with codeigniter

Am having difficulties uploading images with codeigniter it keeps saying that i didn't select a file to upload.
this is my code
function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'jpg|png';
$config['max_size'] = '3000';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
if (!$this->upload->do_upload()){
echo $this->upload->display_errors();
}else{
echo $this->upload->data();
}
}
it doesn't even send the image to the directory.
try this function instead :
function upload_done() {
$config['overwrite'] = TRUE;
$config['encrypt_name'] = FALSE;
$config['remove_spaces'] = TRUE;
$config['upload_path'] = '/var/www/uploadfiles/'; // use an absolute path
$config['allowed_types'] = 'jpg|png';
$config['max_size'] = '0';
if ( ! is_dir($config['upload_path']) ) die("THE UPLOAD DIRECTORY DOES NOT EXIST");
$this->load->library('upload',$config);
if ( ! $this->upload->do_upload() )
{
echo "UPLOAD ERROR ! ".$this->upload->display_errors();
} else {
echo "THE IMAGE HAS BEEN UPLOADED : "; var_dump( $this->upload->data() );
}
}
Make sure your html looks like below.
<input type="file" name="userfile" size="20" />
Codeigniter's upload library assumes the name to be "userfile"
You need to change the call to do_upload() depending on the name you have given the form input. Lets say you have this code in your HTML:
<input type="file" name="image" />
You need to change:
$this->upload->do_upload()
to
$this->upload->do_upload('image')
By default CodeIgniter assumes the name is "userfile".

Resources