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'];
}
}
Related
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 */
}
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');
}
I have one multi-part form. I want to upload 5 images and one video with this form. I do not want using AJAX upload.
$this->form_validation->set_rules('file' , 'lang:pic' , 'callback_multiple_upload');
$this->form_validation->set_rules('video' , 'lang:video' , 'callback_video_upload');
For multiple images
$config['upload_path'] = PATH; //add path according to your requirements
$config['allowed_types'] = 'jpg|jpeg|png';
$config['overwrite'] = false; //OR true
$config['max_size'] = '100000'; //You can change it
$this->load->library('upload');
$files = $_FILES;
$number_of_files = count($_FILES['pic']['name']); //"pic" is name of FILE input
//images name will be details0, details1, details2 and soo on.
$errors = 0;
for ($i = 0; $i < $number_of_files; $i++) {
$_FILES['pic']['name'] = "details" . $i . ".jpg"; //If you want to change the name of images change "details" with your require name
$_FILES['pic']['type'] = $files['pic']['type'][$i];
$_FILES['pic']['tmp_name'] = $files['pic']['tmp_name'][$i];
$_FILES['pic']['error'] = $files['pic']['error'][$i];
$_FILES['pic']['size'] = $files['pic']['size'][$i];
$this->upload->initialize($config);
if (!$this->upload->do_upload("pic"))
{
$errors++;
}
}
if ($errors > 0)
{
echo $errors . "File(s) could not be uploaded";
}
You can initialize the upload class for each file and set the required file type i.e.
$config['upload_path']= './uploads/files/';
$config['allowed_types'] = 'docx|doc|pdf|txt|odt';
$config['max_size'] = 10000;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->upload->initialize($config);
$this->upload->do_upload();
//check errors in first upload.
then for video
$config['upload_path']= './uploads/videos/';
$config['allowed_types'] = 'mp4|flv';
$config['max_size'] = 10000;
$this->upload->initialize($config);
$this->upload->do_upload();
For multiple file uploading
$filesCount = count($_FILES['userFiles']['name']);
for($i = 0; $i < $filesCount; $i++){
$_FILES['userFile']['name'] = $_FILES['userFiles']['name'][$i];
$_FILES['userFile']['type'] = $_FILES['userFiles']['type'][$i];
$_FILES['userFile']['tmp_name'] = $_FILES['userFiles']['tmp_name'][$i];
$_FILES['userFile']['error'] = $_FILES['userFiles']['error'][$i];
$_FILES['userFile']['size'] = $_FILES['userFiles']['size'][$i];
$upload_path ="/test/assets/upload";
$output_dir = $_SERVER['DOCUMENT_ROOT']. $upload_path;
$config['upload_path'] = $output_dir;
// $config['upload_path'] = $uploadPath;
$config['allowed_types'] = 'jpg|png|mp4; //add other_extensions';
$config['file_name'] = 'PIC_'.$user_id.'_'.$i;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if($this->upload->do_upload('userFile')){
$fileData = $this->upload->data();
$img_insert_array = array('user_id'=>$user_id,'profile_pic'=> $fileData['file_name']);
$res = $this->Test_model->update_img($img_insert_array );
//here insert in db
echo "uploaded successful";
}
else {
print_r($this->upload->display_errors());
}
}
I have an issue with codeigniter file upload library. I am trying to upload multiple files (7 images), i renamed them and after which uploaded to my server file system. But i cannot get the new name to save in my database.
Here is my Controller
if (isset($_POST['carform']))
{
$dir = './uploads/cars';
$config['upload_path'] = $dir;
$config['allowed_types'] = 'jpg|jpeg|png';
$config['max_size'] = '2048';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$config['remove_spaces'] = 'TRUE';
$uid = $this->tank_auth->get_user_id();
$config['file_name'] = time().$uid;
$this->load->library('upload', $config);
foreach($_FILES as $field => $file)
{
if($file['error'] == 0)
{
// So lets upload
if ($this->upload->do_upload($field))
{
$data = array('upload' => $this->upload->data());
$this->cars_model->set_car($data);//end foreach loop.....
$id = $this->db->insert_id();
redirect('/cars/details/'.$id.'');
}else
{
$errors = $this->upload->display_errors();
echo($errors);
}
}
}
}//end if statement ...
You do have a helper function within Codeigniter File Uploading Class: $this->upload->data(). In order to get the filename do this:
$my_data=$this->upload->data();
$file_name=$my_data['filename'];
check the manual here
edit:
to insert data into database assuming $filename is a string you need first explode it into an array
$filename="14010989464.jpg 140109894641.jpg 140109894642.jpg 140109894643.jpg 140109894644.jpg 140109894645.jpg 140109894646.jpg";
$fn=explode(' ',$filename);
then you can insert the data (imagename) into the column img of your table test like this:
foreach ($fn as $key=>$val){
$data['img']=$fn[$key];
$this->db->insert('test', $data);
}
can use for
ex :
$this->load->library('upload', $config);
for($i=1;$i<=5;$i++) //5 = total image
{
if (!$this->upload->do_upload('userfile'.$i))
{
$data['error'] = array('error' => $this->upload->display_errors());
}
else
{
$upload_data = $this->upload->data();
$data[$i] = $upload_data['file_name'];
}
}
$data_x = array(
'image1' => $data['1'],
'image2' => $data['2'],
'image3' => $data['3'],
'image4' => $data['4'],
'image5' => $data['5'],
);
I am trying to create a multiple image uploader and I have come across this link. What I am confused about in relation to my code below and the link is that do I have to have 2
$this->upload->do_upload(); functions to run my code or how do I use
$this->upload->initialize($config); in the below situation?
Code:
//Image Upload Function
$conceptOne = 'conceptOne';
$conceptTwo = 'conceptTwo';
$location = $_SERVER['DOCUMENT_ROOT'].'/_assets/quote/uploads/';
$folderName = $this->quote->getCompanyDetails()->companyName;
$folderName = str_replace(" ", "_", $folderName);
$folderName = strtolower($folderName);
if(!is_dir($location.$folderName))
{
mkdir($location.$folderName);
chmod($location.$folderName, 0777);
//Set File Settings
$config['upload_path'] = $location.$folderName;
$config['allowed_types'] = 'jpg|png|pdf';
$config['file_name'] = $conceptOne;
$config['remove_spaces'] = TRUE;
$config['overwrite'] = TRUE;
$config['max_size'] = '1024';
$config['max_width'] = '1024';
$config['max_height'] = '768';
$this->load->library('upload', $config);
print_r($config);
if(!$this->upload->do_upload($conceptOne)) { #= try upload
$data['uploadError'] = array('uploadError' => $this->upload->display_errors()); #Error
$this->load->view('layout', $data);
} // Do upload
else{
$data = array('upload_data' => $this->upload->data($conceptOne));
}// end else
}// end if folder
You need a loop to reinitialize the file upload library so that you can process some other images that are uploaded by the user.
Let's say a user uploaded 2 images. Then that means you need to put the code that initialize the file upload library and do the file upload inside that loop.
for ($i = 0; $i < 2; $i++)
{
// Change the config here if necessary
$this->upload->initialize($config);
// Call do_upload() here
}
Kemal is right: you have to iterate over the files you have. I would put the "concepts" in an array, so you can use foreach:
// Load upload library without any configuration
$this->load->library('upload');
$concepts = array('conceptOne','conceptTwo');
$location = $_SERVER['DOCUMENT_ROOT'].'/_assets/quote/uploads/';
$folderName = $this->quote->getCompanyDetails()->companyName;
$folderName = str_replace(" ", "_", $folderName);
$folderName = strtolower($folderName);
if(!is_dir($location.$folderName))
{
mkdir($location.$folderName);
chmod($location.$folderName, 0777);
}
$config['upload_path'] = $location.$folderName;
$config['allowed_types'] = 'jpg|png|pdf';
$config['remove_spaces'] = TRUE;
$config['overwrite'] = TRUE;
$config['max_size'] = '1024';
$config['max_width'] = '1024';
$config['max_height'] = '768';
// Upload 'concepts'
foreach($concepts as $concept)
{
$config['file_name'] = $concept;
$this->upload->initialize($config);
$this->upload->do_upload($concept);
}
// Upload logo
$config['file_name'] = 'logo-filename.gif';
$this->upload->initialize($config);
$this->upload->do_upload('logo');