insert multiple images in database using codeigniter and filepond - php

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

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

Upload multiple image doesn't store to databased and path folder

I'm making upload multiple image and stored to one field. But I have a proble with my code, images can't store to database and to path folder. May be you can help me please
This is my controller
public function post() {
if(empty($_FILES['file']['name'])) {
$data = array( 'id_merk' => $this->input->post('id_merk'),
'createdAt' => $this->input->post('createdAt')
);
// var_dump($data);
$this->m_barang->post( $data );
$this->session->set_flashdata('success', 'success');
redirect('admin/merk');
} else {
$count = count($_FILES['file']['size']);
foreach($_FILES as $value){
for($s=0; $s<=$count-1; $s++) {
$_FILES['file']['name'] = $value['name'][$s];
$_FILES['file']['type'] = $value['type'][$s];
$_FILES['file']['tmp_name'] = $value['tmp_name'][$s];
$_FILES['file']['error'] = $value['error'][$s];
$_FILES['file']['size'] = $value['size'][$s];
// $config['file_name'] = 'pict_'.date('Y_m_d_H_i_s').'.jpg';
$config['upload_path'] = './upload/be/barang';
$config['allowed_types'] = 'gif|GIF|jpg|JPG|jpeg|JPEG|png|PNG';
$config['max_size'] = '8000';
$config['max_width'] = '1366';
$config['max_height'] = '1024';
$this->load->library('upload', $config);
$this->upload->do_upload();
$data = $this->upload->data();
$name_array[] = $data['file_name'];
}
}
$names = implode(', ', $name_array);
$data = array( 'id_merk' => $this->input->post('id_merk'),
'photo_barang' => $names
);
$this->m_barang->post( $data );
$this->session->set_flashdata('success', 'gambar ada');
redirect('admin/merk');
}
}
and this is my view
<input class="form-control" name="file[]" id="files" type="file" multiple="multiple">
Please, can you help me how to fix my problem with my code?
First, Use only 1 loop to upload the multiple images.
Second, give name attribute of <input type="file"> to $this->upload->do_upload()
Third, if uploading path inside the application folder then use APPPATH.'upload/be/barang' or if outside the application folder use FCPATH.'upload/be/barang'
$count = count($_FILES['file']['size']);
for($s=0; $s<=$count-1; $s++) {
$_FILES['file']['name'] = $_FILES['file']['name'][$s];
$_FILES['file']['type'] = $_FILES['file']['type'][$s];
$_FILES['file']['tmp_name'] = $_FILES['file']['tmp_name'][$s];
$_FILES['file']['error'] = $_FILES['file']['error'][$s];
$_FILES['file']['size'] = $_FILES['file']['size'][$s];
$config['upload_path'] = './upload/be/barang';
$config['allowed_types'] = 'gif|GIF|jpg|JPG|jpeg|JPEG|png|PNG';
$config['max_size'] = '8000';
$config['max_width'] = '1366';
$config['max_height'] = '1024';
$this->load->library('upload', $config);
if(!$this->upload->do_upload('file')){
//image uploading error
}else{
$data = $this->upload->data();
$name_array[] = $data['file_name'];
}
}

How to upload multiple files with multiple inputs in codeigniter

How to upload multiple files with multiple inputs in codeigniter. Below is my code. I want to add many files but with different inputs for files.
if(!empty($_FILES['countryfile']['name']))
{
$filesCount = count($_FILES['countryfile']['name']);
for($i = 0; $i < $filesCount; $i++)
{
$imgFile=$_FILES['countryfile']['name'][$i];
$tmp_dir=$_FILES['countryfile']['tmp_name'][$i];
$imgSize=$_FILES['countryfile']['size'][$i];
//$upload_dir='../uploads/dish_images';
$imgExt=strtolower(pathinfo($imgFile,PATHINFO_EXTENSION));
$valid_extensions = array('jpeg', 'jpg', 'png', 'gif','pdf');
$image=rand(1000,10000).".".$imgExt;
$config['upload_path'] = '../admin/upload_doc';
//$config['upload_path'] = 'http://teq-staging.com/maswad-phase2/admin/uploads/dish_images/';
$config['allowed_types'] = 'jpg|jpeg|png|gif|pdf';
$config['file_name'] = $_FILES['countryfile']['name'];
$upload_dir=$config['upload_path'];
//$config['file_name']=$image;
$upload_dir=$config['upload_path'];
$this->upload->initialize($config);
if($this->upload->do_upload('countryfile')){
$upload_data = $this->upload->data();
}
}
upload form.php
<?php echo form_open_multipart('upload'); ?>
<p>
<?php echo form_label('File 1', 'userfile') ?>
<?php echo form_upload('userfile') ?>
</p>
<p>
<?php echo form_label('File 2', 'userfile1') ?>
<?php echo form_upload('userfile1') ?>
</p>
<p><?php echo form_submit('submit', 'Upload them files!') ?></p>
<?php form_close() ?>
the controller
function index()
{
// Has the form been posted?
if (isset($_POST['submit']))
{
// Load the library - no config specified here
$this->load->library('upload');
// Check if there was a file uploaded - there are other ways to
// check this such as checking the 'error' for the file - if error
// is 0, you are good to code
if (!empty($_FILES['userfile']['name']))
{
// Specify configuration for File 1
$config['upload_path'] = 'uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
// Initialize config for File 1
$this->upload->initialize($config);
// Upload file 1
if ($this->upload->do_upload('userfile'))
{
$data = $this->upload->data();
}
else
{
echo $this->upload->display_errors();
}
}
// Do we have a second file?
if (!empty($_FILES['userfile1']['name']))
{
// Config for File 2 - can be completely different to file 1's config
// or if you want to stick with config for file 1, do nothing!
$config['upload_path'] = 'uploads/dir2/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '100';
$config['max_width'] = '1024';
$config['max_height'] = '768';
// Initialize the new config
$this->upload->initialize($config);
// Upload the second file
if ($this->upload->do_upload('userfile1'))
{
$data = $this->upload->data();
}
else
{
echo $this->upload->display_errors();
}
}
}
else
{
$this->load->view("upload_form");
}
}

How to store the file in mysql database using codeigniter?

I have created a simple contact form.I want to store the file in database which is uploaded by any user.I am able to store the file in my folder but i have to store the file in database to identify the admin which file is upload.
Controller
public function index()
{
$this->load->view('demo', array('error' => ' ' ));
}
public function do_upload()
{
$id = $this->session->userdata('id');
$this->load->model('Model_doc');
$config['upload_path'] = './upload/';
$config['allowed_types'] = 'docx';
$config['max_size'] = 1000;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
$this->upload->do_upload();
$this->Model_doc->index($id, $this->upload->data());
}
Model
public function index($id,$imgdata)
{
$imgdata = file_get_contents($imgdata['../upload/']);
$data = array(
'path'=>$imgdata,
);
$this->db->set('curentDate', 'NOW()', FALSE);
$this->db->insert('test',$data);
}
View
<?php echo form_open_multipart('welcome/do_upload');?>
<input type="file" name="userfile" size="20" />
<br /><br />
<input type="submit" value="upload" />
</form>
Change code in the Controller File as Folow:
public function do_upload()
{
$id = $this->session->userdata('id');
$this->load->model('Model_doc');
$config['upload_path'] = './upload/';
$config['allowed_types'] = 'docx';
$config['max_size'] = 1000;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
$this->upload->do_upload();
$full_file_path = base_url()."/upload/".$_FILES['userfile']['name'];
//This Is Full Path of the file stored to your folder,Change it if required.
$this->Model_doc->index($id,$full_file_path);
}
Changes in the Model File:
public function index($id,$full_file_path)
{
$data = array(
'path'=>$full_file_path,
);
$this->db->set('curentDate', 'NOW()', FALSE);
$this->db->insert('test',$data);
}

multiple file upload codeigniter

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.

Categories