I am using the function below to upload my image files to my server(Localhost). It is fine and image is being uploaded. But I need two image fields and hence both the images should be uploaded once the submit button is clicked. I used the function described here Codeigniter multiple file upload , but it is not working.
I get this error message
A PHP Error was encountered
Severity: Warning
Message: is_uploaded_file() expects parameter 1 to be string, array given
Filename: libraries/Upload.php
Line Number: 161
Well I cannot understand where the error is.
The function that I am using to upload single image is
function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'jpeg|png|gif';
$config['max_size'] = '0';
$config['max_width'] = '0';
$config['max_height'] = '0';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
//failed display the errors
}
else
{
//success
}
}
In addition I also like to ask can i change the input field name of my choice. i.e i always need to implement <input type="file" name="userfile"/> . Is it possible to change the name? I tried changing it and I get the message No file was selected, so that must mean that I cannot change it.
You have to loop through the uploaded files like it is shown in your provided link.
function do_upload() {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'jpeg|png|gif';
$config['max_size'] = '0';
$config['max_width'] = '0';
$config['max_height'] = '0';
$this->load->library('upload', $config);
foreach ($_FILES as $key => $value) {
if (!empty($value['tmp_name'])) {
if ( ! $this->upload->do_upload($key)) {
$error = array('error' => $this->upload->display_errors());
//failed display the errors
} else {
//success
}
}
}
}
And try using HTML like this:
<input type="file" name="file1" id="file_1" />
<input type="file" name="file2" id="file_2" />
<input type="file" name="file3" id="file_3" />
You can change the names of the input fields as you like.
If you are using file multi-select, then do this:
HTML (HTML5 file input with array name allows to select multiple files):
<input type="file" name="userfile[]"/>
or
<input type="file" name="userfile[]"/>
<input type="file" name="userfile[]"/>
PHP in CodeIgniter:
// load upload library (put your own settings there)
$this->load->library('upload', array('allowed_types' => 'svg|gif|jpg|png', 'upload_path' => $GLOBALS['config']['upload_path'], ));
// normalise files array
$input_name = 'userfile'; // change it when needed to match your html
$field_names = array('name', 'type', 'tmp_name', 'error', 'size', );
$keys = array();
foreach($field_names as $field_name){
if (isset($_FILES[$input_name][$field_name])) foreach($_FILES[$input_name][$field_name] as $key => $value){
$_FILES[$input_name.'_'.$key][$field_name] = $value;
$keys[$key] = $key;
}
}
unset($_FILES[$input_name]); // just in case
foreach ($keys as $key){
$new_file = $this->upload->do_upload($input_name.'_'.$key);
// do your stuff with each uploaded file here, delete for example:
$upload_data = $this->upload->data();
unlink($upload_data['file_name']);
}
Related
Here I am attaching the code of the desires problem.
Cotroller has following code.
Controller=>
//Load upload library
$this->load->library('upload');
$images = array();
$i = 0;
foreach ($_FILES as $key => $value)
{
$tmp = explode(".",$value['name'][$i]);
$imagename = time().".".end($tmp);
$_FILES['file']['name'] = $imagename;
$_FILES['file']['type'] = $value['type'][$i];
$_FILES['file']['tmp_name'] = $value['tmp_name'][$i];
$_FILES['file']['error'] = $value['error'][$i];
$_FILES['file']['size'] = $value['size'][$i];
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['file_name'] = $imagename;
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('file'))
{
$error = array($i => $this->upload->display_errors());
echo "<pre>";print_r($error);die;
}
else
{
array_push($images,$this->upload->data()['file_name']);
}
$i++;
}
echo "<pre>";print_r($images);die;
This is a form code that I am using while uploading file.
View =>
<?php $attributes = array(
"class" => "form-horizontal m-t-20",
"method" => "post",
"novalidate" => "",
"enctype" => "multipart/form-data"
);
echo form_open('admin/user/adduser', $attributes); ?>
Here is my file input control.
<label for="file">Profile Images*</label>
<input type="file" name="files[]" id="file" multiple required placeholder="Profile Images" class="form-control">
Change your code as follows
foreach($_FILES["files"]["tmp_name"] as $key=>$value) {
and change $i to the $key as follows (apply to the all)
$_FILES['file']['type'] = $_FILES["files"]['type'][$key];
As wazabii suggested, attached some random string to the file name. You can use rand(100,10000)
That is because the time() will be the same on all the images, so the file name is not unique. This is easily fixed by adding the array key to the file name.
$tmp = explode(".",$value['name'][$i]);
$imagename = time()."-".$key.".".end($tmp);
contoller code
public function upload_multiple($field_name,$path){
$this->load->library('upload');
$files = $_FILES;
$cpt = count($_FILES[$field_name]['name']);//count for number of image files
$image_name =array();
for($i=0; $i<$cpt; $i++)
{
$_FILES[$field_name]['name']= $files[$field_name]['name'][$i];
$_FILES[$field_name]['type']= $files[$field_name]['type'][$i];
$_FILES[$field_name]['tmp_name'] = $files[$field_name]['tmp_name'][$i];
$_FILES[$field_name]['error']= $files[$field_name]['error'][$i];
$_FILES[$field_name]['size'] = $files[$field_name]['size'][$i];
$this->upload->initialize($this->set_upload_options($path));//for initalizing configuration for each image
$this->upload->do_upload($field_name);
$data = array('upload_data' => $this->upload->data());
$image_name[]=$data['upload_data']['file_name'];//store file name to store in database
}
return $image_name;//all images name which is uploaded
}
public function set_upload_options($path)
{
$config = array();
$config['upload_path'] = $path;
$config['allowed_types'] = 'gif|jpg|png';
$config['overwrite'] = FALSE;
return $config;
}
function call
$image_name=$this->upload_multiple('portfolio_image',$path);//for multiple image upload
input field
<input type="file" id="portfolio_image" name="protfolio_image[]" >
i am unable to upload multiple
Here is my view form
<form method="POST" action="<?=base_url()?>register/saverecord" enctype="multipart/form-data">
<input type="file" name="file_upload[]" multiple="multiple" value=""><br/><br/>
<input type="submit" name="submit" value="SUBMIT">
</form>
here is my code for multiple upload
public function saveRecord() {
$config['upload_path'] = APPPATH . './uploads/';
$path = $config['upload_path'];
$config['allowed_types'] = '*';
$config['max_size'] = '1024';
$config['max_width'] = '1920';
$config['max_height'] = '1280';
$this->load->library('upload', $config);
$fileName = [];
foreach ($_FILES as $fieldname => $fileObject) //fieldname is the form field name
{
if (!empty($fileObject['name'])) {
$this->upload->initialize($config);
if (!$this->upload->do_upload($fieldname)) {
$errors = $this->upload->display_errors();
} else {
$fileName[] = $this->upload->data();
}
}
}
echo "<pre>";
print_r($fileName);
echo "</pre>";
exit;
}
Here is my error message i am getting after upload
I followed this url Upload multiple files in CodeIgniter
if (!$this->upload->do_upload($fieldname)) {
Here fieldname is an array, you need to have individual files here instead of array.
Try Out This code, It will work. You are passing array to do_upload function. That is not valid. I corrected the code please check after replace this code.
public function saveRecord() {
$config['upload_path'] = APPPATH . './uploads/';
$path = $config['upload_path'];
$config['allowed_types'] = '*';
$config['max_size'] = '1024';
$config['max_width'] = '1920';
$config['max_height'] = '1280';
$this->load->library('upload', $config);
$fileName = [];
foreach ($_FILES as $fieldname => $fileObject) //fieldname is the form field name
{
if (!empty($fileObject['name'])) {
$this->upload->initialize($config);
if (!$this->upload->do_upload($fileObject['name'])) {
$errors = $this->upload->display_errors();
} else {
$fileName[] = $this->upload->data();
}
}
}
echo "<pre>";
print_r($fileName);
echo "</pre>";
exit;
}
When processing a multiple file upload you can access the various files like this - how you tie that in with the native methods available to you via codeigniter I don't know
foreach( $_FILES[ 'fieldname' ] as $i => $void ){
$name=$_FILES[ 'fieldname' ]['name'][$i];
$tmp=$_FILES[ 'fieldname' ]['tmp_name'][$i];
$size=$_FILES[ 'fieldname' ]['size'][$i];
$type=$_FILES[ 'fieldname' ]['type'][$i];
/* other code */
}
Trying to read the file name of a file that is uploaded as part of a form. The print_r command that I'm using to test just shows a blank screen. I have read the manual (near the bottom here) pertaining to this and don't understand what I'm doing wrong.
Controller:
public function do_upload() {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|txt|pdf|xlsx|csv|xls|bmp';
$config['max_size'] = 1000;
$this->load->library('upload', $config);
$file_name = $this->upload->data('file_name');
print_r($file_name);
View:
<?php echo form_open_multipart('Corpmuns/do_upload', array('method' => 'post'));?>
... // some drop-down menus and text fields here
<INPUT TYPE="file" NAME="userfile" id="userfile" >
</form>
You didn't do the upload action so the file was not uploaded yet. That's why you can't get the uploaded file's name. Because it does not exists.
Code $this->upload->do_upload('userfile') and make sure the file is uploaded successfully before you get the filename.
Call do_upload function from upload library.
Update Controller:
public function do_upload() {
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|txt|pdf|xlsx|csv|xls|bmp';
$config['max_size'] = 1000;
$this->load->library('upload', $config);
if ($this->upload->do_upload('userfile')) { //use this function
$data['error'] = false;
$upload_data = $this->upload->data();
$data['data'] = $upload_data;
$data['msg'] = 'Image Successfully Uploaded.';
} else {
$data['msg'] = $this->upload->display_errors('', '<br>');
}
print_r($data)
}
if ( ! $this->upload->do_upload('input_name'))
{
echo $this->upload->display_errors();
}
else
{
$file=$this->upload->data();
echo $image=$file['file_name'];//Set file name to varilable
}
}
<form action="" enctype="multipart/form-data" method="post"
name="uploadfile">
you should add enctype="multipart/form-data"
I am trying to upload a file using codeigniter and no matter what I have tried I am getting back the error message that I have not selected a file when I actually have.
My controller looks like so
public function editHeader()
{
$this->require_auth();
$config['upload_path'] = './files/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000KB';
$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());
var_dump($error);
}
else
{
$data = array('upload_data' => $this->upload->data());
echo('yeah!');
}
}
and my html is just a codeigniter form
<?foreach($header as $row) :?>
<?=form_open_multipart('admin/editHeader');?>
<input class="waitForLoad fadeInDown" type="file" name="userfile" />
<input type="submit" name="submit" value="Change" class="btn btn-success pull-right" />
</form>
<?endforeach;?>
I have tried the normal solutions such as upping the max_size as well as putting the upload name in the do_upload function, but nothing seems to work and I am about to pull my hair out. Any help is appreciated!
You need to use form_open_multipart() instead of form_open() when uploading files.
You must use
<?php echo form_open_multipart(path); ?> because we get the $_FILES in controller if we write in the form.
After adding it, if u print_r($_FILES) then you will get the array.
It is necessary when we do the work related to image upload or captcha
How to do a multiple file upload in codeigniter
<input type="file" name="pic[]">
<input type="file" name="pic[]">
<input type="file" name="pic[]">
How can I upload this?
using the do_upload function
You can upload any number of files
$config['upload_path'] = 'upload/Main_category_product/';
$path=$config['upload_path'];
$config['allowed_types'] = 'gif|jpg|jpeg|png';
$config['max_size'] = '1024';
$config['max_width'] = '1920';
$config['max_height'] = '1280';
$this->load->library('upload');
foreach ($_FILES as $key => $value)
{
if (!empty($key['name']))
{
$this->upload->initialize($config);
if (!$this->upload->do_upload($key))
{
$errors = $this->upload->display_errors();
flashMsg($errors);
}
else
{
// Code After Files Upload Success GOES HERE
}
}
}
You can see There is no need of name property.