Controller code
$config['upload_path'] = './uploads/'.$random;
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 100;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('imageupload'))
{
$error = array('error' => $this->upload->display_errors());
}
else
{
$data = array('upload_data' => $this->upload->data());
}
print_r($data);
HTML code:
<form role="form" enctype="multipart/form-data" accept-charset="utf-8" name="formname" id="formname" method="post" action="http://www.example.com/Property/post">
<input type="file" class="default" id="imageupload1" name="imageupload[]">
</form>
Whenever I am uploading file it displaying warning is_uploaded_file() expects parameter 1 to be string, array given. How can I resolve it?
try this
//place this code in your function for multiple image upload
$data = array();
if(!empty($_FILES['imageupload']['name']))
{
$filesCount = count($_FILES['imageupload']['name']);
for($i = 0; $i < $filesCount; $i++){
$_FILES['imageupload']['name'] = $_FILES['imageupload']['name'][$i];
$_FILES['imageupload']['type'] = $_FILES['imageupload']['type'][$i];
$_FILES['imageupload']['tmp_name'] = $_FILES['imageupload']['tmp_name'][$i];
$_FILES['imageupload']['error'] = $_FILES['imageupload']['error'][$i];
$_FILES['imageupload']['size'] = $_FILES['imageupload']['size'][$i];
$uploadPath = 'uploads/';
$config['upload_path'] = $uploadPath;
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if($this->upload->do_upload('imageupload')){
$fileData = $this->upload->data();
$uploadData[$i]['file_name'] = $fileData['file_name'];
$uploadData[$i]['created'] = date("Y-m-d H:i:s");
$uploadData[$i]['modified'] = date("Y-m-d H:i:s");
}
}
if(!empty($uploadData)){
//Insert file into the database using your model
}
}
Probably your input name should be without brackets:
<input type="file" class="default" id="imageupload1" name="imageupload">
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'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'];
}
}
I am trying to upload a file and replace its name with the title name, but I am unable to get the file name by echo in controller even not in as a POST in profiler.
I also need to rename it but before that I need to know the post value ie file name.
Here I am posting my code.
My view
<?php echo form_open_multipart('emailtemplate/do_upload');?>
<tr class='odd gradeX'>
<td class='center'>
<input type="file" name="userfile" size="20" />
</td>
<td class='center'>
<input placeholder='Title For Your File' name='title' class='form-control'>
<td class='center'>
<input placeholder='Comment On File' name='comment' class='form-control'>
</td>
</tr>
<input type="submit" value="upload" />
</form>
My Controller
function do_upload()
{
$this->load->helper('form');
$this->load->helper('html');
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '1000';
$config['encrypt_name'] = true;
//$file=$this->input->post('userfile');
$this->load->library('upload', $config);
$this->upload->data();
$file_name = $this->upload->do_upload('userfile');
echo $file_name;
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('add/addfile', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('add/addfile', $data);
}
$this->output->enable_profiler(true);
}
To rename uploaded file with POST value:
//get extension
$ext = end(explode(".", $_FILES["userfile"]['name']));
$config['file_name'] = $this->input->post("title").'.'.$ext;
Remove $config['encrypt_name'] = true;. Otherwise your file name will be encrypted .
Make sure that your title doesn't not exist in your uploaded folder. Or append some random number / characters with title.
I have created a function to upload the file :-
it takes argument i.e field_name
public function fileUpload($field) {
//get file extension
$exts = #split("[/\\.]", $_Files[$field][name]);
$n = count($exts) - 1;
$ext = $exts[$n];
//create image name or replace it with your desired name
$imageName = date("Ymdhis") . time() . rand();
$config['file_name'] = $imageName; //set the desired file name
$config['overwrite'] = true;
$config['allowed_types'] = str_replace(',', "|", $ext);
$config['upload_path'] = ABSOLUTEPATH . 'product/original/';
$config['optional'] = true;
$config['max_size'] = '1000';
$config['max_width'] = '00';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload($field, true)) {
$error = array('error' => $this->upload->display_errors());
$r = $error;
} else {
$data = array('upload_data' => $this->upload->data());
$r = "sucess";
}
return $r;
}
the bellow i have written code it can be helpful you
$config['upload_path'] = './uploads/path';
$config['allowed_types'] = 'png|jpg|jpeg|gif';
$config['max_size'] = '1000000';
$config['max_width'] = '1000000';
$config['max_height'] = '1000000000';
$this->load->library('upload', $config);
$files = $_FILES;
if(isset($files['file']['name']))
{
$filename = $files['file']['name'];
$ext = end(explode(".", $filename));
$_FILES['file']['name']= date('d').date('m').date('y').'_'.date(time()).'.'.$ext; //here i am changing file name with current date as your wish to change your logic
// print_r($_FILES);exit; check the changed name
if ($this->upload->do_upload('file'))
{
$upload = array('upload_data' => $this->upload->data());
$imagename = $upload['upload_data']['file_name']; //uploaded your image name
}
else
{
$error = array('error' => $this->upload->display_errors());
print_r($error); //error
}
}
NOTE:Hey user3345331 , See the line
$this->upload->do_upload()
Replace With this
$this->upload->do_upload('userfile');
This userfile is the ID of Input box with filetype in your HTML code.
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.