Reputation: 583
I'm trying to use two file upload buttons in codeigniter like below
<label class="control-label" for="default">PO File</label>
<input type="file" id="po_file" name="po_file" multiple="multiple" >
<label class="control-label" for="default">Invoice File</label>
<input type="file" id="in_file" name="in_file" multiple="multiple" >
in controller
$file1 = $_FILES['po_file']['name'];
$file2 = $_FILES['in_file']['name'];
$config['upload_path'] = $pathToUpload;
$config['allowed_types'] = 'pdf';
$config['overwrite' ] =TRUE;
$config['max_size'] =0;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
echo $this->upload->display_errors();
// $this->load->view('file_view', $error);
}
else
{
$this->upload->do_upload(file1);
$upload_data = $this->upload->data();
$file_name = $upload_data['file_name'];
}
I tried like this but it gave You did not select a file to upload. Any help how to do this??? thank you
Upvotes: 2
Views: 2799
Reputation: 949
Seeing your form in the question, I'm assuming that you want two files to be uploaded from two different input fields. Is that it ?
So, doing it in your way, your form should be as :
<form enctype="multipart/form-data" method="post"> <!-- enctype="multipart/form-data" is must, method 'post' or 'get' is depend on your requirement -->
<?php
if( !empty( $notification ) )
{
echo '
<p>Notifications : </p>
<p>'.$notification.'</p>'; <!-- For the status of the uploaded files ( error or success ) -->
}
?>
<label for="default">PO File</label>
<input type="file" name="po_file"> <!-- no need to add "multiple" attribute, unless you want multiple files to be uploaded in the same input field-->
<label for="default">Invoice File</label>
<input type="file" name="in_file">
<input type="submit" name="upload">
</form>
And your controller should be as :
class Image extends CI_Controller {
private $data; // variable to be used to pass status of the uploaded files ( error or success )
function __construct()
{
// some code
}
public function upload()
{
$this->data['notification'] = '';
if( $this->input->post('upload') ) // if form is posted
{
// setting the config array
$config['upload_path'] = 'uploads/'; // $pathToUpload ( in your case )
$config['allowed_types'] = 'pdf';
$config['max_size'] = 0;
$this->load->library('upload', $config); // loading the upload class with the config array
// uploading the files
$this->lets_upload( 'po_file' ); // this function passes the input field name from the form as an argument
$this->lets_upload( 'in_file' ); // same as above, function is defined below
}
$this->load->view('form', $this->data); // loading the form view along with the member variable 'data' as argument
}
public function lets_upload( $field_name ) // this function does the uploads
{
if ( ! $this->upload->do_upload( $field_name )) // ** do_upload() is a member function of upload class, and it is responsible for the uploading files with the given configuration in the config array
{
$this->data['notification'] .= $this->upload->display_errors(); // now if there's is some error in uploading files, then errors are stored in the member variable 'data'
}
else
{
$upload_data = $this->upload->data(); // if succesful, then infomation about the uploaded file is stored in the $upload_data variable
$this->data['notification'] .= $upload_data['file_name']." is successfully uploaded.<br>"; // name of uploaded file is stored in the member variable 'data'
}
}
}
Now suppose, you want a new image file to be uploaded in a different location or whatever, from the same form; then in the config array, you only have to change the array elements which you want to be different as :
$config['upload_path'] = '/gallery';
$config['allowed_types'] = 'gif|jpg|jpeg|png';
Then you have to initialize the config array as :
$this->upload->initialize($config); // *** this is important ***
and then you have to load the upload class with this new config as :
$this->load->library('upload', $config);
and now you can call the lets_upload() function :
$this->lets_upload( 'img_file' );
in the upload() function.
Upvotes: 3
Reputation: 194
If you see upload.php library file under system folder then you'll know that CI takes 'userfile' field name as default, So when you do
if ( ! $this->upload->do_upload())
{
echo $this->upload->display_errors();
// $this->load->view('file_view', $error);
}
//Passing parameter empty, then CI search for 'userfile'.
Try passing field name as you have done in else condition, or setting one of the input field name to 'userfile'.
Upvotes: 0
Reputation: 124
first in your php.ini file_uploads = On
Second be sure of that
<form action="controller/action" method="post" enctype="multipart/form-data">
third check that on codeigniter documentaion about upload file . https://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html
don't forget the extension of files allowd
Upvotes: 0