CI multiple file upload - cannot upload more than 2 files - codeigniter

This function cannot upload more than 2 images. If tried produces error,
Message : undefined index:userfile
View
<input name="userfile[]" id="userfile" type="file" multiple="" />
Controller
function do_upload() {
$count = count($_FILES['userfile']['size']);
foreach($_FILES as $key=>$value){
for($n=0; $n<=$count-1; $n++) {
$_FILES['userfile']['name']=$value['name'][$n];
$_FILES['userfile']['type'] = $value['type'][$n];
$_FILES['userfile']['tmp_name'] = $value['tmp_name'][$n];
$_FILES['userfile']['error'] = $value['error'][$n];
$_FILES['userfile']['size'] = $value['size'][$n];
$config['upload_path'] = './images/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = 0;
$this->load->library('upload', $config);
$this->upload->do_upload();
$data = $this->upload->data();
}
}
}

You should definitely look into your php.ini max post/upload size. Look for values like:
; Maximum allowed size for uploaded files.
upload_max_filesize = ##M
; Must be greater than or equal to upload_max_filesize
post_max_size = ##M
and change the # to values that will suit you

Related

How to validate upload file only pdf

I would like to validate file upload, the condition is file input can upload file if file input is empty, but if file input does not match the type of file setting then the error showing. How to make these error. if I using this script : !$this->upload->do_upload('file') and i let file input empty, certainly the error are showing.
this is my Config upload :
$config['upload_path'] = '../../upload/file';
$config['allowed_types'] = 'pdf';
$config['file_name'] = 'file_'.time();
$config['overwrite'] = true;
$this->upload->initialize($config);
if(!$this->upload->do_upload('file')){
$respond['error'] = true;
$respond['message'] = "Error Upload".$this->upload->display_errors();
}
You could add a condition, so it will only validate if there are uploaded file :
<?php
if ( $_FILES && $_FILES['file']['name'] )
{
$config['upload_path'] = '../../upload/file';
$config['allowed_types'] = 'pdf';
$config['file_name'] = 'file_'.time();
$config['overwrite'] = true;
$this->upload->initialize($config);
if(!$this->upload->do_upload('file')){
$respond['error'] = true;
$respond['message'] = "Error Upload".$this->upload->display_errors();
}
}
This solution will work perfectly for you
$config['upload_path'] = './uploads/;
$config['allowed_types'] = 'pdf';
$config['max_size'] = 0;
$config['max_width'] = 0;
$config['max_height'] = 0;
$config['file_name'] = round(100,99); //make your file name random
$this->load->library('upload', $config);
$this->upload->initialize($config);
if($this->upload->do_upload('file_name')){
print $this->upload->data();
}else{
print $this->upload->display_errors();
}

codeigniter upload images from different inputs

my code is working fine in the single upload, but I want to make a loop for my code that I won't repeatedly create a function over and over because of using two different inputs.
public function uploadImage_1()
{
$config['upload_path'] = './uploads';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '2608';
$config['max_width'] = '2608';
$config['max_height'] = '2608';
$this->load->library('upload', $config);
if ( !$this->upload->do_upload('userfile1')){
$error = array('error' => $this->upload->display_errors());
}else{
$fileName = $this->upload->data();
$post_image = $fileName['file_name'];
return $post_image;
}
}
public function uploadImage_2()
{}
` View
<input type="file" name="userfile1" size="20" required/>
<input type="file" name="userfile2" size="20" required/>`
You can pass the input field name as parameter into your function.
like this:
public function uploadImage($input_field_name)
{
$config['upload_path'] = './uploads';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = '2608';
$config['max_width'] = '2608';
$config['max_height'] = '2608';
$this->load->library('upload', $config);
if ( !$this->upload->do_upload($input_field_name)){
$error = array('error' => $this->upload->display_errors());
}else{
$fileName = $this->upload->data();
$post_image = $fileName['file_name'];
return $post_image;
}
}
Now use the above function like this:
// These variables stores the name of your files
$fileName_1 = $this->uploadImage('userfile1');
$fileName_2 = $this->uploadImage('userfile2');

codeigniter upload files from multiple input

i want to upload files with multiple input. I have tried the code below, but the files not uploaded, and i cant get the upload directory.
HTML
<input type="file" name="userfile[]" size="20" />
Controller
public function detailTugasInput($idKelas,$id){
$this->load->model('tugass');
$records = $_FILES['userfile'];
//var_dump($records); die();
$config['upload_path'] = './file/';
$config['allowed_types'] = 'pdf|doc|txt|jpg|png';
$config['max_size'] = 0;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$this->load->library('upload', $config);
foreach ($_FILES as $key => $value) {
$upload_data = $this->upload->data();
$file_ary = array(
'nama_kembali' => $value['name'],
'file_kembali' => $value['tmp_name']
);
}
}
And i also tried to get the uploaded data with this code below, but why always appear error?
$checked_arr = $_POST['userfile'];
Try Below Codes
HTML :
<input type="file" class="form-control" name="userfile[]" multiple />
Controller :
public function detailTugasInput($idKelas, $id){
$this->load->model('tugass');
if(!empty($_FILES['userfile']['name'])) {
$filesCount = count($_FILES['userfile']['name']);
for($i = 0; $i < $filesCount; $i++) {
$_FILES['userFile']['name'] = $_FILES['userfile']['name'][$i];
$_FILES['userFile']['type'] = $_FILES['userfile']['type'][$i];
$_FILES['userFile']['tmp_name'] = $_FILES['userfile']['tmp_name'][$i];
$_FILES['userFile']['error'] = $_FILES['userfile']['error'][$i];
$_FILES['userFile']['size'] = $_FILES['userfile']['size'][$i];
$config['upload_path'] = './file/';
$config['allowed_types'] = 'pdf|doc|txt|jpg|png';
$this->load->library('upload', $config);
$this->upload->initialize($config);
$this->upload->do_upload('userFile');
if($this->upload->do_upload('userFile')){
$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");
}
}
}
}
For more Support, Check This

While Uploading Image don't want to save actual image name with space

In codeigniter while I uploading the image and store image name in database If actual image name is like "abc data.jpg" and I want to store image name like abc_data.jpg and also with this name image should move in uploads folder.
This is my image Upload Code:-
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = 5000;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ($this->upload->do_upload('cover_image'))
{
$data =$this->upload->data();
$data_ary = array(
'project_id' => (int)$iProjectId,
'image_url' => $data['file_name'],
'is_covred_photo' => 'YES'
);
$this->db_project->insert( $this->sTable6 , $data_ary);
$aResp = array( 'project_images_id' => $this->db->insert_id());
}
Add $config['file_name']
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|jpeg';
$config['max_size'] = 5000;
$config['file_name'] = 'My_new_file_name'; # Add here
$this->load->library('upload', $config);
remove this line as well
$this->upload->initialize($config);
Try this:
$file_name = $_FILES['file_view_name']['name'];
$file_name_pieces = split('_', $file_name);
$new_file_name = '';
$count = 1;
foreach($file_name_pieces as $piece)
{
if ($count !== 1)
{
$piece = ucfirst($piece);
}
$new_file_name .= $piece;
$count++;
}
$config['file_name'] = $new_file_name;
Refer this

how to upload different files (images & documents) in codeigniter

hi i'm new to codeigniter, can you please help me in insert multiple files(documents and images) in code igniter. here's my sample code
view:
<label>Picture</label>
<input type="file" name="userfile" size="100" />
<label>Document</label>
<input type="file" name="documentfile" size="10" />
controller:
$m = $_FILES['userfile']['name'];
$n = $_FILES['documentfile']['name'];
if ($m !== "")
{
$config['upload_path'] = './upload_images/';
$config['allowed_types'] = 'jpg|png|jpeg|gif';
$config['max_size'] = '0'; // 0 = no file size limit
$config['max_width'] = '0';
$config['max_height'] = '0';
$config['overwrite'] = TRUE;
$this->load->library('upload', $config);
$this->upload->do_upload();
$upload_result = $this->upload->data();
}
elseif ($n !== "")
{
$config_document['upload_path'] = './upload_documents/';
$config_document['allowed_types'] = 'pdf';
$config_document['max_size'] = '0';
$config_document['overwrite'] = TRUE;
$this->load->library('upload', $config_document);
$this->upload->do_upload();
$upload_result2 = $this->upload->data();
}
$image_filename = $upload_result['file_name'];
$docu_filename = $upload_result2['file_name '];
$this->MODEL->add_asset($image_filename, $docu_filename);
i tried to echo both file names and it works but my $docu_filename generates NULL value;
please help. thank you
hi its simple just check the file extension of uploaded file and make settings according to that. one more thing you have to set your html form enctype also. check the following example
form View
<form method="post" action="controller" enctype="multipart/form-data">
<input type="file" name="test">
<input type="submit" value="submit" />
</form>
in controller
$path = $_FILES['test']['name'];
$ext = pathinfo($path, PATHINFO_EXTENSION);
$img_ext_chk = array('jpg','png','gif','jpeg');
$doc_ext_chk = array('pdf','doc');
if (in_array($ext,$img_ext_chk))
{
$config['upload_path'] = './upload_images/';
$config['allowed_types'] = 'jpg|png|jpeg|gif';
$config['max_size'] = '0'; // 0 = no file size limit
$config['max_width'] = '0';
$config['max_height'] = '0';
$config['overwrite'] = TRUE;
$this->load->library('upload', $config);
$this->upload->do_upload();
$upload_result = $this->upload->data();
}
elseif (in_array($ext,$doc_ext_chk))
{
$config_document['upload_path'] = './upload_documents/';
$config_document['allowed_types'] = 'pdf';
$config_document['max_size'] = '0';
$config_document['overwrite'] = TRUE;
$this->load->library('upload', $config_document);
$this->upload->do_upload();
$upload_result2 = $this->upload->data();
}

Resources