uploading .ini files in CI - php

I get following error when trying to upload .ini files
"The filetype you are attempting to upload is not allowed."
Below is the function for uploading files
private function _upload_config_file() {
$config['upload_path'] = APPPATH . 'config/ini/';
$config['allowed_types'] = 'ini';
$config['max_size'] = '2000000';
$this->load->library('upload', $config);
if ($this->upload->do_upload()) {
$this->upload_status = 'File uploaded succesfully !';
return true;
} else {
$this->upload_status = $this->upload->display_errors();
return false;
}
}
In View
$upload_file_attr = array(
'name' => 'userfile',
'id' => 'userfile',
'disabled' => 'disabled'
);
echo form_open_multipart('sys_setting', $form_sys_setting_multi);
echo form_label('Configure Manually' . form_checkbox($upload_checkbox, '', FALSE, $js_checkbox));
echo form_button($download_button);
echo form_upload($upload_file_attr);
echo form_reset($upload_reset);
echo form_submit($upload_submit);
echo form_close();
Is there any changes to be made in php.ini for uploading .ini files ?
Or Is it codeigniter's upload library that doesn't allow to upload .ini files ?
Need suggestions to solve this issue.

you forgot to put the field_name
Try adding this:
if ($this->upload->do_upload('userfile')) { //parameter should the your input file name attribute
$this->upload_status = 'File uploaded succesfully !';
return true;
} else {
$this->upload_status = $this->upload->display_errors();
return false;
}

I got this issue solved.In config/mimes.php modified $mimes array
$mimes = array('ini' => 'application/octet-stream')
If any one knows how to solve this issue without modifying the core files, *Please do comment.*

Related

file name changes when i upload i file using do_upload function

when i upload a file using do_upload() in code igniter, it uploads the file but it renames it with underscore(_) in the place of blank spaces.
file name to be uploaded: A B C.pdf
file that uploads: A_B_C.pdf
code in controller to upload file:
//$filefeild is the name of folder in which file is to be saved
public function upload_Files($filefeild)
{
//set preferences
$config['upload_path'] = 'assets/bcc/'.$filefeild."/";
$config['allowed_types'] = 'doc|docx|xml|pdf|image|excel|jpg|png|jpeg';
$config['max_size'] = 0;
$config['filname'] = $filenamee = $_FILES[$filefeild]['name'];
$this->upload->initialize($config);
if (!$this->upload->do_upload($filefeild))
{
// case - failure
$upload_error = array('error' => $this->upload->display_errors());
}
else
{
// case - success
$upload_data = $this->upload->data();
$data['success_msg'] = '<div class="alert alert-success text-center">Your file <strong>' . $upload_data['file_name'] . '</strong> was successfully uploaded!</div>';
$filname = $upload_data['file_name'];
return $filenamee;
}
}
IF you see codeigniter's function exist here, you will find following code,
// Remove white spaces in the name
if ($this->remove_spaces === TRUE)
{
$this->file_name = preg_replace('/\s+/', '_', $this->file_name);
}
Which means codeigniter removes spaced and adds _ by default which can be changed by setting config variable. like $config['remove_spaces'] = FALSE;
Secondly if you want your file should be saved with underscors and you get actual name by which file is being saved then in your code you have to make a slight change as described below.
your current code
$config['filname'] = $filenamee = $_FILES[$filefeild]['name'];
Change it to
$config['filname'] = $_FILES[$filefeild]['name'];
and in success scenario get file name from $this->upload->data()
Note: you can find answere here aswell.

Files not getting stored in correct folder

I have a form in CodeIgniter that allows the user to upload 2 separate files. At the backend I want these files to get stored in the different folder. In the controller i have written the upload code
public function upload()
{
/**Start uploading file**/
$config['upload_path'] = './assets/file/.';
$config['allowed_types'] = 'gif|jpg|png|doc|txt';
$config['max_size'] = 1024 * 8;
$config['encrypt_name'] = TRUE;
$this->load->library('upload', $config);
if (!$this->upload->do_upload('file'))
{
$error = array('error' => $this->upload->display_errors());
echo $error;
}
else
{
$data = $this->upload->data();
echo $file = $data['file_name']; //name of file
}
/**End uploading file**/
/**Start uploading img**/
$config2['upload_path'] = './assets/img/.';
$config2['allowed_types'] = 'gif|jpg|png|doc|txt';
$config2['max_size'] = 1024 * 8;
$config2['encrypt_name'] = TRUE;
$this->load->library('upload', $config2);
if (!$this->upload->do_upload('img'))
{
$error1 = array('error' => $this->upload->display_errors());
echo $error1;
}
else
{
$data1 = $this->upload->data();
echo $img = $data1['file_name']; //name of img
}
/**End uploading img**/
}
The images are getting uploaded but they are getting uplaoded to same folder. Can anyone please tell how i can make the files get saved in seperate folders
in the second load , you need to initialiaze the loaded library because the load method don't initialize it :
$this->upload->initialize($config2);

How do i upload .ini file in codeigniter?

Hi I am trying to upload .ini file in Codeigniter but CI shows me error that this file type is not allowed.
I tried putting this in mimes.php but didn't work :
$mimes = array('ini' => 'application/octet-stream')
My code is:
public function index()
{
$this->load->view('customer/upload/upload_ini', array('error' => ' ' ));
}
function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'ini';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('customer/upload/upload_ini', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('customer/upload/upload_success', $data);
}
}
This solution may work for you but it will
upload all filetypes without any extension check,
$config['allowed_types'] = '*';
May this link will be useful to you.
Hope it will help you :)

codeigniter file is not uploading on remote server

this is a code for swf fiel uploading but the problem is it works fine on localhost but not in the remote server.here is my controller
function do_upload()
{
$pid=$this->input->post('Page_id');
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'swf';
$config['max_size'] = '1048';
$config['file_name'] =$pid;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
}
else{
$data=array(
'id'=>$pid,
'link'=>base_url()."uploads/",
'file_name'=>$config['file_name']
);
$this->file_upload_model->upload_data($data);
$this->upload_success();
}
}
i have not done anything except just uploading the path and file name to the database in model. see the demo here
Try this
$pid=$this->input->post('Page_id');
$config['file_name'] = $pid;
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'swf';
$config['max_size'] = '1048';
$config['remove_spaces'] = TRUE;
$this->load->library('upload', $config);
if ($this->upload->do_upload())
{
//upload successful
//do something
}
else
{
$this->session->set_flashdata('error',$this->upload->display_errors());
redirect('controller/method');
/*
In the view file you can echo the error like this
echo $this->session->flashdata('error');
*/
}
You have pass the the field name in the do_upload call like this:
$config = array(
'allowed_types' => 'jpg|jpeg|swf |png', // pipe seperated file types
'upload_path' => './uploads/', // should be the root path
'max_size' => '1048',
'file_name' => $pid
);
$this->load->library('upload', $config);
if (!$this->upload->do_upload('your_field_name')) {
// like $this->upload->do_upload($pid)
$error = array('error' => $this->upload->display_errors());
} else {
$swf_data = $this->upload->data();
}
Hope it makes sense
it may be due to PHP server version and it's option.
1).go to cpanel account
2).click "select php version", in the software section.
3).tap the "fileinfo" chexbox in the php option and save.
now you can upload file perfectly.

how to unzip uploaded zip file?

I am trying to upload a zipped file using codeigniter framework with following code
function do_upload()
{
$name=time();
$config['upload_path'] = './uploadedModules/';
$config['allowed_types'] = 'zip|rar';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_view', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->library('unzip');
// Optional: Only take out these files, anything else is ignored
$this->unzip->allow(array('css', 'js', 'png', 'gif', 'jpeg', 'jpg', 'tpl', 'html', 'swf'));
$this->unzip->extract('./uploadedModules/'.$data['upload_data']['file_name'], './application/modules/');
$pieces = explode(".", $data['upload_data']['file_name']);
$title=$pieces[0];
$status=1;
$core=0;
$this->addons_model->insertNewModule($title,$status,$core);
}
}
But the main problem is that when extract function is called, it extract the zip but the result is empty folder. Is there any way to overcome this problem?
$zip = new ZipArchive;
$res = $zip->open($fileName);
if($res==TRUE)
{
$zip->extractTo($path.$fileName);
echo "<pre>";
print_r($zip);//to get the file type
$zip->close();
try this :
<?php
exec('unzip filename.zip');
?>
Hmm.., I think you set an incorrect path of your uploaded zip file OR your destination path ('./application/modules/') is incorrect.
Try this :
$this->unzip->extract($data['upload_data']['full_path'], './application/modules/');
I use this -> $data['upload_data']['full_path'], to make sure that it's a real path of the uploaded file.
Hope it helps :)
same problem i faced few min back.if you observe carefully you find
please copy zip file and paste to folder contain programe file(.php) after that you
i think file is not store in temp folder.
if(preg_match("/.(zip)$/i", $fileName))
{
$moveResult= move_uploaded_file($fileTmpLoc, $fileName);
if($moveResult == true)
{
$zip = new ZipArchive;
$res = $zip->open($fileName);
if($res==TRUE)
{
$zip->extractTo($path.$fileName);
echo "<pre>";
print_r($zip);
$zip->close();
} else {
echo 'failed';
}
}
unlink($fileName); // Remove the uploaded file from the PHP temp folder
//exit();
}`
class Upload extends CI_Controller {
function __construct(){
parent::__construct();
// load ci's Form and Url Helpers
$this->load->helper(array('form', 'url'));
}
function index(){
$this->load->view('upload_form_view', array('error' => ' ' ));
}
function file_upload(){
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'zip';
$config['max_size'] = '';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload()){
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form_view', $error);
}else{
$data = array('upload_data' => $this->upload->data());
$zip = new ZipArchive;
$file = $data['upload_data']['full_path'];
chmod($file,0777);
if ($zip->open($file) === TRUE) {
$zip->extractTo('./uploads/');
$zip->close();
echo 'ok';
} else {
echo 'failed';
}
$this->load->view('upload_success_view', $data);
}
}
}
In case anyone comes here for same question, just add chmod($file,0777); to the original code posted yetAnotherSE. That solves the issue of empty files.

Categories