I'm trying to upload a file on CodeIgniter 2 but it fails. I tried a few different ways, actually CI is getting the file and trying to put it in destination folder but I think there is a problem with folder permissions. Because I can echo file name etc.
This is my model file's related part:
$config = array
(
'upload_path' => 'uploads/campaigns/data',
'allowed_types' => 'csv',
);
$this->upload->initialize($config);
$this->upload->do_upload('file');
$file = $this->upload->data();
$file_name = $file['file_name'];
if(!empty($file_name)) {
echo $file_name;
}
else {
echo $this->upload->display_errors();
}
With this code, I can see filename but file is not uploading. I only have root account on this server and all files created and owned by root. Folders have 777 permission. What should I try now?
Thank you!
Use this to track your errors
if ( ! $this->upload->do_upload())
{
echo $this->upload->display_errors();
}
else
{
echo "success";
}
I hope you loaded the library as well
EDIT 01
$config['upload_path'] = './uploads/campaigns/data';
$config['allowed_types'] = 'csv';
$config['max_size'] = '5120'; # 5MB allowd
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
echo $this->upload->display_errors();
}
else
{
echo $this->upload->data();
}
And in application/config/mimes.php
Check there is a csv format. If ot exist add this on array as new item
'csv' => array('text/x-comma-separated-values', 'text/comma-separated-values', 'application/octet-stream', 'application/vnd.ms-excel', 'application/x-csv', 'text/x-csv', 'text/csv', 'application/csv', 'application/excel', 'application/vnd.msexcel'),
I think there is an issue with Upload library of the Codeigniter. See link for more details and I hope this will help you.
Related
I have a problem with the xls and xlsx mimes types. I'm trying to upload an excel file but I get string(130)
"application/vnd.openxmlformats-officedocument.spreadsheetml.sheetapplication/vnd.openxmlformats-officedocument.spreadsheetml.sheet"
but i have that type in mi xlsx mime
'xlsx' => array('application/vnd.openxmlformats-officedocument.spreadsheetml.sheet', 'application/zip', 'application/vnd.ms-excel', 'application/msword', 'application/x-zip'),
This is my upload controller
public function agregararchivo() {
if ($_FILES["userfile"]['name']=='') {
// Here you can directly redirect to the form page itself with the Error Message
} else {
$new_name = time().$_FILES["userfile"]['name']; //This line will be generating random name
$config['upload_path'] = FCPATH ."assets/imagenusuario/";
$config['allowed_types'] = 'xlsx|xls';
$config['file_name'] = $new_name;
$this->load->library('upload', $config); //Loads the Uploader Library
$this->upload->initialize($config);
if (!$this->upload->do_upload('userfile')) {}
else {
$data = $this->upload->data();
}
}
}
I don't know what is wrong, if I have that type in the mimes it should work but it doesn't.
Just had the same problem with mime_content_type()
It's a PHP bug, you can see it here: https://bugs.php.net/bug.php?id=77784
I'm setting up an upload form in CodeIgniter. I've setup the allowed_types parameter to:
$config['allowed_types'] = 'xls|xlsx';
And added the following line to config/mimes.php:
'xls' => array('application/excel', 'application/vnd.ms-excel', 'application/msexcel'),
'xlsx' => 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
However, despite the file I'm trying to upload being an xlsx file CI is still telling me the filetype isn't allowed. Any thoughts?
public function do_upload() {
// Change this if you change the upload directory
$upload_path = "/webusers/ad6vcf/public_html/funding_manager/application/uploads";
$config['upload_path'] = $upload_path;
//At some point will add support for csv
$config['allowed_types'] = 'xls|xlsx';
//Max 5000kb please
$config['max_size'] = '5000';
$this->load->library('upload', $config);
$this->upload->initialize($config);
if (!$this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$this->upload($error);
} else {
$upload_data = $this->upload->data();
bulk_insert($upload_path . $upload_data['file_name']);
}
}
Please try this
$this->load->library('upload', $config);
$this->upload->initialize($config);
Have you tried this?
if (!$this->upload->do_upload('put the name of the input field here'))
I have written code for uploading a file in CodeIgniter.
Here is the view file code
<form method='post' type="get" enctype="multipart/form-data" action="" class="apply_form" id="apply_form">
<input type="file" name="submit_resume" id="submit_resume" class="submit_resume" value="browse"/>
Here is the controller file code
$this->load->helper('url');
$this->load->helper('html');
$this->load->helper('form');
$this->load->library('upload');
$config['upload_path'] = $this->base_url.'/uploads/';
$config['allowed_types'] = 'docx|doc|pdf';
$config['max_size'] = '1000';
//$config['max_width'] = '1024';
//$config['max_height'] = '768';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
//$this->load->view('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
//$this->load->view('upload_success', $data);
}
But nothing is uploading in the uploads folder.
Cant figure out what the issue really is!
Some changes I would do:
$config['upload_path'] = './uploads/';
Mention only the relative path and not absolute path.
if ( ! $this->upload->do_upload('submit_resume')) //here see the file name
{
$error = array('error' => $this->upload->display_errors());
print_r( $error );die;
}
Just to view the error you are getting.
You must have the correct permission set in your uploads folder too.
The permission of the folder in which the file is getting uploaded might be the issue.
$config['upload_path'] = './uploads/';
For changing the permission of above folder, use following command in your terminal -
chmod -R 750 /uploads/
Explanation:
chmod 750 changes to permission to read and write for the user.
-R makes is recursive.
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.*
I am trying to upload video in server using Codeigniter, In the localhost i can do it, but when in the server it does not work. Even though do_upload() function returns true.
When i'm testing the server root, print_r($_SERVER['DOCUMENT_ROOT']); it returns
/var/www/mjpp/data/www/supps.mydomainname.com
Also the
$updata = $this->upload->data(); returns
[file_path] => /var/www/mjpp/data/www/supps.mydomainname.com/public/video
Can you tell me why /var/www/mjpp/data/ is appearing before my main domain name , and is it the cause of any error for uploading video?
Thank you in advance, sorry for my bad english.
/var/www/mjpp/data/www/supps.mydomainname.com is absolute location of your document root in a system in general. However you do not usually have access to anything above your user dir.
public/video or /var/www/mjpp/data/www/supps.mydomainname.com/public/video (as absolute path) is what you pass to Upload library as upload_path. This is where the library will try yo upload the files. This folder should exist and be writeable (have 0777 permissions on it for example).
No need to use Document Root, Codeigniter itself having configurations when file uploading.
Here is sample code for image uploading,
function do_upload()
{
$config['upload_path'] = './uploads/';
$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())
{
$error = array('error' => $this->upload->display_errors());
$this->load->view('upload_form', $error);
}
else
{
$data = array('upload_data' => $this->upload->data());
$this->load->view('upload_success', $data);
}
}
You just need to change the configuration($config array), for your issue you should use the $config['upload_path'].
Follow codigniter file upload tutorial
Dont use $_SERVER['DOCUMENT_ROOT'] in CI, use URL Helper
$this->load->helper('url');
echo base_url();