multiple file upload codeigniter - php

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.

Related

is_uploaded_file() expects parameter 1 to be string, array given while upload multiple files in codeigniter

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 */
}

insert multiple images in database using codeigniter and filepond

Following is my controller In this I am using two if statements one for multiple images and another is for the featured image.. my images are uploaded in a folder very well but multiple names are not inserted in the database...Only one file name is inserted in the database...
public function uploadApi()
{
if (isset($_FILES['userfile'])) {
$config['upload_path'] = 'uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 200000;
$config['max_width'] = 2024;
$config['max_height'] = 1768;
$this->upload->initialize($config);
$this->load->library('upload', $config);
$this->upload->do_upload('userfile');
$data = array( $this->upload->data());
$this->m->update_post($data[0]['file_name']);
}
if(isset($_FILES['userfile1'])) {
$config['upload_path'] = 'uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 200000;
$config['max_width'] = 2024;
$config['max_height'] = 1768;
$this->upload->initialize($config);
$this->load->library('upload', $config);
$this->upload->do_upload('userfile1');
$data = array( $this->upload->data());
$this->m->update_feature($data[0]['file_name']);
}
}
This is a model ..
#-Update images Post-#
public function update_post($picture) {
$post = array(
'post_images'=>$picture,
);
$this->db
->where('post_status','draft')
->update('post',$post);
return true;
}
public function update_feature($picture) {
$post = array(
'post_featured_image'=>$picture,
);
$this->db
// ->set('post_created', 'NOW()', FALSE)
->where('post_status','draft')
->update('post',$post);
return true;
}
filepond plugin script
FilePond.registerPlugin(
FilePondPluginFileValidateSize,
FilePondPluginImageExifOrientation,
FilePondPluginImageCrop,
FilePondPluginImageResize,
FilePondPluginImagePreview,
FilePondPluginImageTransform
);
// Set default FilePond options
FilePond.setOptions({
// maximum allowed file size
maxFileSize: '50MB',
imagePreviewHeight: 100,
imagePreviewWidth: 200,
instantUpload: true,
// crop the image to a 1:1 ratio
imageCropAspectRatio: '1:1',
// upload to this server end point
server: {
url: '<?php echo base_url() ?>Admin/uploadApi',
}
});
var pond = FilePond.create(document.querySelector('input[name="userfile"]'));
var pond = FilePond.create(document.querySelector('input[name="userfile1"]'));
**This is a view ..**
<form method="post" enctype="multipart/form-data" class="toggle-disabled" action="<?php echo base_url() ?>Admin/update_post1" id='ritesh'>
<div class="col-md-6">
<div class="form-group">
<label>Upload images</label>
<input type="file"
class="filepond"
name="userfile"
multiple
data-max-file-size="5MB"
data-max-files="50" data-validation="required extension" />
</div>
<div class="form-group">
<label>Feature image</label>
<input type="file"
class="filepond"
name="userfile1"
data-max-file-size="5MB"
data-validation="required extension"
/>
</div>
</form>
For multiple image upload you should post images array like; imagename[]. Your current approach is not good.
You must try already posted answers:
Multiple image upload with CodeIgniter
Multiple image upload with Codeigniter saving only one file path to MySQL Database
https://www.codexworld.com/codeigniter-upload-multiple-files-images/
Please try to this in controller
function uploadApi() {
$image = $_FILES;
foreach ($image as $key => $img) {
if (!is_dir('./Uploads/')) {
mkdir('./Uploads/', 0777, TRUE);
}
if (!empty($img['name'])) {
$config['upload_path'] = './Uploads/Products/';
$config['allowed_types'] = '*';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['overwrite'] = TRUE;
$config['file_name'] = date('U') . '_' . $img['name'];
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload($key)) {
$error = array('error' => $this->upload->display_errors());
print_r($error);
die;
} else {
if ($this->upload->do_upload($key)) {
$image_data = $this->upload->data();
$update["userfile"] = $config['file_name'];
$res = $this->m->update_post($update);
}
}
}
}
$this->load->view('imgtest');
}

Codeigniter - Need help uploading picture to both folder and database

I need help uploading the picture to the folder and database. It seems I can only send it to the database, but not upload it to the folder. Is there anything wrong with the code?
admin.php (controller)
public function input_siswa(){
$config['upload_path'] = './gambarfolder/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload');
$this->upload->initialize($config);
$location=base_url().'/gambarfolder/';
$pict=$location.$data_imge;
$nama_siswa = $this->input->post('nama_siswa');
$nis = $this->input->post('nis');
$id_jurusan = $this->input->post('id_jurusan');
$data = array(
'nama_siswa'=>$nama_siswa,
'nis'=>$nis,
'id_jurusan'=>$id_jurusan,
'gambar'=>$pict
);
$this->m_model->create('siswa',$data);
redirect(base_url('index.php/admin/tampil_siswa'));
}
Upload image in codeigniter using upload library.
public function input_siswa(){
$config['upload_path'] = './gambarfolder/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$location=base_url().'/gambarfolder/';
$pict=$location.$data_imge;
$this->load->library('upload', $config);
if ($this->upload->do_upload('userfile'))
{
$nama_siswa = $this->input->post('nama_siswa');
$nis = $this->input->post('nis');
$id_jurusan = $this->input->post('id_jurusan');
$data = array(
'nama_siswa'=>$nama_siswa,
'nis'=>$nis,
'id_jurusan'=>$id_jurusan,
'gambar'=>$pict
);
$this->m_model->create('siswa',$data);
redirect(base_url('index.php/admin/tampil_siswa'));
// redirect succsess page when image upload succsessfully.
}else{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload', $error);
// return image upload error.
}
}
first check your folder gambarfolder is in the root of your codigniter application
second check in your form tag you add enctype="multipart/form-data"
and add that lines for upload image to gambarfolder folder
$this->load->library('upload', $config);
if($this->upload->do_upload('userfile'))
{
}
where userfile is the name of file tag like
<input type="file" name="userfile" size="20" />
and $config is your config array
and also check https://www.codeigniter.com/userguide3/libraries/file_uploading.html
You need to add below line after initialise config
$this->upload->do_upload('userfile')

uploading images in codeigniter, is userfile required?

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']);
}

multi upload images by codeigniter and create thumb

I have a problem , I took me 2 hours but I couldn't solve it.
When I want to upload multiple images (3 images ) it will upload but then I can't create a thumb for all , just for the first picture , I don't know where is the problem
this is my form
<form action="" method="post" enctype="multipart/form-data">
<input type="file" id="hotel_img1" value="upload" name="hotel_img1">
<input type="file" id="hotel_img2" value="upload" name="hotel_img1">
<input type="file" id="hotel_img3" value="upload" name="hotel_img1">
<input type="submit">
</form>
this is my code
controller
<?php
if(!empty($_FILES))
$upload_image = array('1','2','3','4','5');
foreach($upload_image as $i) {
if(!empty($_FILES["hotel_img$i"]['name']))
{
$hotel_ID= 12;
$config['file_name'] = $this->session->userdata('user_id').'-'.$hotel_ID.'-'.time().$i;
$config['upload_path'] =realpath(APPPATH . '../img');
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '512';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['overwrite'] = TRUE;
$this->upload->initialize($config);
$form_name = 'hotel_img' . $i;
if(!$this->upload->do_upload($form_name))
{
$data['msg'] .= "ERROR";
}
else
{
$file_data = $this->upload->data();
$image_data['img_url'] = $file_data['file_name'];
$image_data['img_size'] = $file_data['file_size'] * 1024;
$image_data['img_forID'] = $hotel_ID;
$this->model_hotels->insert_image($image_data);
$data['msg'] .= "Uploaded Picture No $i";
$config_thumb['image_library'] = 'gd2';
$config_thumb['source_image'] = $file_data['full_path'];
$config_thumb['maintain_ratio'] = FALSE;
$config_thumb['width'] = 250;
$config_thumb['height'] = 150;
$config_thumb['new_image'] = realpath(APPPATH . '../img').'/thumb/thumb_' . $file_data['file_name'];
$this->load->library('image_lib', $config_thumb);
$this->image_lib->resize();
$this->image_lib->clear();
}
}
}
$this->model_hotels->set_hotel_thumb($hotel_ID);
} ?>
try adding this to your config:
$config['create_thumb']
Seems to me that $file_data is always the same and probably contains an array with the other pictures as well. Now your code is saying for every picture resize $file_data and it only picks the first picture uploaded. Try print_r($file_data) because i guess that's what is causing the problem.

Categories