How to store uploaded file into folder using CodeIgniter? - codeigniter

How to store upload file into folder using CodeIgniter?

Please Be sure with your gallery folder it must be in root alongside with application folder name gallery
$config['upload_path']='./gallery/';
$config['allowed_types']='gif|png|jpeg|jpg';
$config['max_size']='100';
$config['max_width']='1024';
$config['max_height']='700';
$this->upload->initialize($config);
$this->upload->do_upload('upload_photo');

Hey try this code inc controller. This the function when u hit the upload button in your vie form..
function sendresume()
{
$config['upload_path'] = 'C:\xampp\htdocs\upload\application'; //path where to save in the systme
$config['allowed_types'] = 'doc|docx|pdf'; //file types to accept while uplaoding
$config['max_size'] = '10240'; //size limit
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
echo "Resume Successfully uploaded to database.............";
$file = $data['upload_data']['full_path']; //upload file path
}
}

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 remove existing file when uploading new one in codeigniter file uploading?

I am using codeigniter file uploading class to upload files and images in my local folder.I am using the code below, it's working fine.
public function do_upload() {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg|pdf|txt|sql';
$config['max_size'] = 8048000;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile')) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('Emp_details_view', $error);
}
else {
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
}
Now, my question is how to remove previously added file when uploading new one.
You can use unlink() function to delete your previous image
unlink('image-path/image-name');
first save your filename in a database table then when unlinking take that value from database and unlink. here uploads is the foldername .
unlink("uploads/".$val['image']);
$val['image'] contains imagename from db.

How to upload image file using codeigniter?

I want upload image file on server & want to stored image in database.
How can I store image file in database & also get the image file from database. Please help to solved this problem.
i have done this code in project..
but when uploading image on server upload_path not found.
In controller
$config['upload_path'] = base_url().'aplication/upload/'; // the uploaded images path
$config['allowed_types'] = 'jpg|jpeg|png';/*types of image extentions allowed to be uploaded*/
$config['max_size'] = '3048';// maximum file size that can be uploaded (2MB)
$this->load->library('upload',$config);
if ( ! is_dir($config['upload_path']) ) /* this checks to see if the file path is wrong or does not exist.*/
{
echo( $config['upload_path']);
die("THE UPLOAD DIRECTORY DOES NOT EXIST"); // error for invalid file path
$this->load->library('upload',$config); /* this loads codeigniters file upload library*/
}
if ( !$this->upload->do_upload() )
{
$error=array('error'=>$this->upload->display_errors());
//echo "UPLOAD ERROR ! ".$this->upload->display_errors(); //image error
// $this->load->view('main_view',$error);
}
else
{
$file_data=$this->upload->data();
$data['img']=base_url().'.application/upload/'.$file_data['file_name'];
echo("Image upload successfully..");
//================================================
// $this->load->model('insert_model');
// $this->insert_model->submit_image();
//==========================================
//$this->insert_model->insert_images($this->upload->data());
// $this->load->model('insert_model');
//$this->insert_model->submit_image();
}
That is quite easy. Just create image upload function once in your controller. Here is one function I wrote:
function image_upload($path, $size, $width, $height, $new_name, $ext)
{
$config['upload_path'] = $path;
$config['allowed_types'] = 'jpg|png';
$config['max_size'] = $size;
$config['file_name'] = time() . $new_name . "." . $ext;
$config['max_width'] = $width;
$config['max_height'] = $height;
$this->load->library('upload', $config); //Load codeigniter's file upload library
if (!$this->upload->do_upload())
{
return false;
} else
{
return $config['file_name'];
}
}
Now in the action function of form where you have file input field call this function as follows do as follows:
if ($_FILES['userfile']['name'] != "")//Check if file was selected by the user
{
$ext = pathinfo($_FILES['userfile']['name'], PATHINFO_EXTENSION);//Get the extension of file
$picture_name = $this->image_upload("your/upload/directory/", 10000, 10000, 100000, "test", $ext);
//Now save this $picture_name in database
}
else
{
print_r($this->upload->display_errors());//Display errors if file does not get uploaded successfully
}
Thats it.
Read This, then you will get how to upload file.
Now how to store it into DB.!
In controller use this :
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$img_data = array('upload_data' => $this->upload->data());
$full_url = "/uploads/".$img_data['upload_data']['file_name'];
$reply = $this->model_name->upload_image($full_url);
}
In model using upload_image($full_url) function store image name with path, so that you can get back that path whenever needed.

codeigniter upload function not uploading to directory

I've followed the codeigniter example when it comes to uploading a file, but for some reason it is not working. I've created the folder uploads under public_html folder immediately. I've checked also the flow of the code using echo statements, and it is reaching within the if statement shown below, yet still nothing is showing in the directory uploads which is set to permission 0777.
if($_FILES)
{
//handling the upload
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '480';
$config['max_height'] = '270';
$this->load->library('upload', $config);
if ( ! $this->courseImage())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('new/image', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('error', $data);
}
}else{
$this->load->view('new/image');
}
Appreciate your support and I am still learning codeigniter, so forgive me if it is too easy of a problem.
I did something else and this worked for me. I followed the exact function names provided in codeigniter docs and it worked. I created a method called do_upload and called it using $this->do_upload. This actually worked well.
Thanks

How to upload image in CodeIgniter?

In view
<?php echo form_open_multipart('welcome/do_upload');?>
<input type="file" name="userfile" size="20" />
In controler
function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['overwrite'] = TRUE;
$config['encrypt_name'] = FALSE;
$config['remove_spaces'] = TRUE;
if ( ! is_dir($config['upload_path']) ) die("THE UPLOAD DIRECTORY DOES NOT EXIST");
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('userfile')) {
echo 'error';
} else {
return array('upload_data' => $this->upload->data());
}
}
And I call this function like this
$this->data['data'] = $this->do_upload();
and view this image:
<ul>
<?php foreach ($data['upload_data'] as $item => $value):?>
<li><?php echo $item;?>: <?php echo $value;?></li>
<?php endforeach; ?>
</ul>
I don't know what's the error.
It seems the problem is you send the form request to welcome/do_upload, and call the Welcome::do_upload() method in another one by $this->do_upload().
Hence when you call the $this->do_upload(); within your second method, the $_FILES array would be empty.
And that's why var_dump($data['upload_data']); returns NULL.
If you want to upload the file from welcome/second_method, send the form request to the welcome/second_method where you call $this->do_upload();.
Then change the form helper function (within the View) as follows1:
// Change the 'second_method' to your method name
echo form_open_multipart('welcome/second_method');
File Uploading with CodeIgniter
CodeIgniter has documented the Uploading process very well, by using the File Uploading library.
You could take a look at the sample code in the user guide; And also, in order to get a better understanding of the uploading configs, Check the Config items Explanation section at the end of the manual page.
Also there are couple of articles/samples about the file uploading in CodeIgniter, you might want to consider:
http://code.tutsplus.com/tutorials/how-to-upload-files-with-codeigniter-and-ajax--net-21684
http://runnable.com/UhIc93EfFJEMAADX/how-to-upload-file-in-codeigniter
http://jamshidhashimi.com/image-upload-with-codeigniter-2/
http://code.tutsplus.com/tutorials/how-to-upload-files-with-codeigniter-and-ajax--net-21684
http://hashem.ir/CodeIgniter/libraries/file_uploading.html (CodeIgniter 3.0-dev User Guide)
Just as a side-note: Make sure that you've loaded the url and form helper functions before using the CodeIgniter sample code:
// Load the helper files within the Controller
$this->load->helper('form');
$this->load->helper('url');
// Load the helper files within the application/config/autoload
$autoload['helper'] = array('form', 'url');
1. The form must be "multipart" type for file uploading. Hence you should use `form_open_multipart()` helper function which returns:
``
Simple Image upload in codeigniter
Find below code for easy image upload:
public function doupload()
{
$upload_path="https://localhost/project/profile"
$uid='10'; //creare seperate folder for each user
$upPath=upload_path."/".$uid;
if(!file_exists($upPath))
{
mkdir($upPath, 0777, true);
}
$config = array(
'upload_path' => $upPath,
'allowed_types' => "gif|jpg|png|jpeg",
'overwrite' => TRUE,
'max_size' => "2048000",
'max_height' => "768",
'max_width' => "1024"
);
$this->load->library('upload', $config);
if(!$this->upload->do_upload('userpic'))
{
$data['imageError'] = $this->upload->display_errors();
}
else
{
$imageDetailArray = $this->upload->data();
$image = $imageDetailArray['file_name'];
}
}
//this is the code you have to use in you controller
$config['upload_path'] = './uploads/';
// directory (http://localhost/codeigniter/index.php/your directory)
$config['allowed_types'] = 'gif|jpg|png|jpeg';
//Image type
$config['max_size'] = 0;
// I have chosen max size no limit
$new_name = time() . '-' . $_FILES["txt_file"]['name'];
//Added time function in image name for no duplicate image
$config['file_name'] = $new_name;
//Stored the new name into $config['file_name']
$this->load->library('upload', $config);
if (!$this->upload->do_upload() && !empty($_FILES['txt_file']['name'])) {
$error = array('error' => $this->upload->display_errors());
$this->load->view('production/create_images', $error);
} else {
$upload_data = $this->upload->data();
}
Change the code like this. It works perfectly:
public function uploadImageFile() //gallery insert
{
if($_SERVER['REQUEST_METHOD'] == 'POST') {
$new_image_name = time() . str_replace(str_split(' ()\\/,:*?"<>|'), '',
$_FILES['image_file']['name']);
$config['upload_path'] = 'uploads/gallery/';
$config['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
$config['file_name'] = $new_image_name;
$config['max_size'] = '0';
$config['max_width'] = '0';
$config['max_height'] = '0';
$config['$min_width'] = '0';
$config['min_height'] = '0';
$this->load->library('upload', $config);
$upload = $this->upload->do_upload('image_file');
$title=$this->input->post('title');
$value=array('title'=>$title,'image_name'=>
$new_image_name,'crop_name'=>$crop_image_name);}
$image_folder = APPPATH . "../images/owner_profile/" . $_POST ['mob_no'] [0] . $na;
if (isset ( $_FILES ['image'] ) && $_FILES ['image'] ['error'] == 0) {
list ( $a, $b ) = explode ( '.', $_FILES ['image'] ['name'] );
$b = end ( explode ( '.', $_FILES ['image'] ['name'] ) );
$up = move_uploaded_file ( $_FILES ['image'] ['tmp_name'], $image_folder . "." . $b );
$path = ($_POST ['mob_no'] [0] . $na . "." . $b);
Below code for an uploading a single file at a time.
This is correct and perfect to upload a single file.
Read all commented instructions and follow the code.
Definitely, it is worked.
public function upload_file() {
***// Upload folder location***
$config['upload_path'] = './public/upload/';
***// Allowed file type***
$config['allowed_types'] = 'jpg|jpeg|png|pdf';
***// Max size, i will set 2MB***
$config['max_size'] = '2024';
$config['max_width'] = '1024';
$config['max_height'] = '768';
***// load upload library***
$this->load->library('upload', $config);
***// do_upload is the method, to send the particular image and file on that
// particular
// location that is detail in $config['upload_path'].
// In bracks will set name upload, here you need to set input name attribute
// value.***
if($this->upload->do_upload('upload')) {
$data = $this->upload->data();
$post['upload'] = $data['file_name'];
} else {
$error = array('error' => $this->upload->display_errors());
}
}
check $this->upload->initialize($config); this works fine for me
$new_image_name = "imgName".time() . str_replace(str_split(' ()\\/,:*?"<>|'), '',
$_FILES['userfile']['name']);
$config = array();
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|bmp|jpeg';
$config['file_name'] = $new_image_name;
$config['max_size'] = '0';
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|mp4|jpeg';
$config['file_name'] = url_title("imgsclogo");
$config['max_size'] = '0';
$config['overwrite'] = FALSE;
$this->upload->initialize($config);
$this->upload->do_upload();
$data = $this->upload->data();
}

Resources