files not uploading in codeigniter - php

I am trying to upload 2 files through a codeigniter controller. When i select the files and hit submit it always returns error. But when i do a var_dump($_FILES); it shows that the files are passing but not being captured by the codeigniter controller.
Can someone tell me what i am doing wrong? Below is my code
$config['upload_path'] = './docs/';
$config['allowed_types'] = 'jpg|doc|docx';
$config['max_size'] = 10000;
$config['max_width'] = 3000;
$config['max_height'] = 3000;
$this->load->library('upload', $config);
if ( !$this->upload->do_upload('userfile1') || !$this->upload->do_upload('userfile2'))
{
echo "error";
} else {
$f1= $this->upload->data('userfile1');
$f2= $this->upload->data('userfile2');
echo $f1['file_name'];
echo $f2['file_name'];
}

For multiple file uploading in CI please follow this way --
$config['upload_path'] = 'uploads/photos/';
$config['allowed_types'] = 'jpg|jpeg|png|gif';
$this->load->library('upload', $config);
for ($i=0; $i < count($_FILES['photos']['name']); $i++) {
$_FILES['photos[]']['name'] = $_FILES['photos']['name'][$i];
$_FILES['photos[]']['type'] = $_FILES['photos']['type'][$i];
$_FILES['photos[]']['tmp_name'] = $_FILES['photos']['tmp_name'][$i];
$_FILES['photos[]']['error'] = $_FILES['photos']['error'][$i];
$_FILES['photos[]']['size'] = $_FILES['photos']['size'][$i];
if ($this->upload->do_upload('photos[]')) {
$photos_files = array('upload_data' => $this->upload->data());
$photos_arr[] = $photos_files['upload_data']['file_name'];
}else{
$error[] = $this->upload->display_errors();
}
}

Related

how to upload multi file type with one form in codeigniter?

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

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

Codeigniter Upload Files/image only works when offline

I made a code to upload files on the web, when I was still using xampp, it always going well, but after I upload to hosting, file uploading not working..
here's my code :
MODEL (for updating database record)
function edit_logo_web()
{
$timestamp = date('YmdHi');
$img = $_FILES['userfile']['name'];
$logo = $timestamp."_".$img;
$data = array (
'logo' => $logo
);
$id = 1;
$this->db->where('site.id', $id);
if($this->upload->do_upload()){
$this->db->update('site', $data);}
else{
}
}
CONTROLLER (for uploading img/file to web directory)
function logo_web()
{
$timestamp = date('YmdHi');
$img = $_FILES['userfile']['name'];
$config['file_name'] = $timestamp."_".$img;
$config['upload_path'] ='asset/img/';
$config['allowed_types'] = 'gif|jpg|png|bmp|jpeg|png';
$config['max_size'] = '1024';
$config['max_width'] = '1600';
$config['max_height'] = '1200';
$config['remove_spaces'] = FALSE;
$this->load->library('upload', $config);
$data = array('upload_data' => $this->upload->data());
$this->m_cmm_bimbel->edit_logo_web();
echo "<script>
alert('Success !!');
window.location.href='logo_web';
</script>";
}
it Does very well in xampp localserver..
not in the hostingserver..
Any tips?

Can't upload multiple files with different names in codeigniter

I want to upload two images with my given name
$config['upload_path'] = '/path/to/file';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '0';
$config['file_name'] ='logo1_'.$_POST['name'];
$config['max_width'] = '0';
$config['max_height'] = '0';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('image1'))
{...}
else
{
$data = $this->upload->data();
}
$config2['upload_path'] = '/path/to/file';
$config2['allowed_types'] = 'gif|jpg|png';
$config2['max_size'] = '0';
$config2['file_name'] = 'logo2_'.$_POST['name'];
$config2['max_width'] = '0';
$config2['max_height'] = '0';
$this->load->library('upload', $config2);
if ( ! $this->upload->do_upload('image2'))
{...}
else
{
$data = $this->upload->data();
}
My first image correctly saved in my folder with the name logo1_myname but my second image dosen't save as logo2_myname it save as logo1_myname1 What is wrong with my code ?
Change
$this->load->library('upload', $config2);
to
$this->upload->initialize($config2);
Since the class has already been loaded codeigniter won't load it again.
Hope this helps.

CodeIgniter cyrillic names in files

$config['upload_path'] = './uploads/logo/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = '0';
$config['max_width'] = '0';
$config['max_height'] = '0';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('image_bg'))
{
//$error = array('error' => $this->upload->display_errors());
//$this->load->view('upload_form', $error);
echo 'fail';
}
else
{
//some actions
}
when i upload a file with cyrillic names, they look worse, how can I convert a file name to a windows-1251 unicode after uploading it to the server in codeigniter?
set the file name in config.
$config['file_name'] = 'image_' . time();
$config[0]['upload_path'] = './uploaded_files/banner';
$config[0]['allowed_types'] = 'gif|jpg|png';
$config[0]['file_name'] = $image_id."_ipad";
$config[0]['remove_spaces'] = TRUE;
$config[0]['overwrite'] = TRUE;
//Loading Library - File Uploading
$this->load->library('upload');
$this->upload->initialize($config[0]);
$this->upload->do_upload('banner_image_ipad');
$data = array('upload_data' => $this->upload->data());
$banner_image_ipad = $data['upload_data']['file_name'];

Categories