CodeIgniter FileUpload Not Allowed - php

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'))

Related

Unable to upload these formate file in codeigniter STEP, STP, IGES, IGS, SLDPRT, 3DM, SAT or X_T

Unable to upload these formate file in codeigniter STEP, STP, IGES, IGS, SLDPRT, 3DM, SAT or X_T
public function do_upload()
{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|pdf|step';
$config['max_size'] = 100000000000;
//$config['max_width'] = 1024;
// $config['max_height'] = 768;
$this->load->library('upload', $config);
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('fileUpload'))
{
$error = array('error' => $this->upload->display_errors());
print_r($error);
// echo 'error';
// $this->load->view('upload_form', $error);
}else{
$this->upload->data();
}
error :- The filetype you are attempting to upload is not allowed
for those file types, just change your config of allowed_types into this to override checking extension:
$config['allowed_types'] = "*";
Before uploading your file, codeigniter check your file's extension first, but your file types are not in application/config/mimes.php so codeigniter does not know and then always return FALSE.
Another way is to add your mimes type into mimes.php, you can check out this in this link

File extension is change upper case to lower case in codeigniter

I am uploading file in codeigniter using
$this->upload->do_upload('image')
but when file moves to particular path the file extension is changed (it's changed to lower case)
for example if I upload file "profile.JPG" it's changes to "profile.jpg"
please change CI system library
default in CI upload library $file_ext_tolower = FALSE.
.system\libraries\upload.php
public $file_ext_tolower = TRUE;
do_upload does this
$config['allowed_types'] = 'gif|jpg|png'
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);
}
}
If you want to use same name / extension you send to save your file. You can use :
$upload_dir= $this->config->item("upload_dir");
$fileName = $_POST['sku_code'].".".$extension;
$filePath = $upload_dir.$fileName;
move_uploaded_file($_FILES["image-file"]["tmp_name"],$filePath );
Useful link :
https://ellislab.com/codeigniter/user-guide/libraries/file_uploading.html

Codeigniter Image upload ignore allowed types and file size

I have below code to upload user image, my code work good but I'm getting issue while checking allowed_types i.e. png|jpg and max_size..
It doesn't check allowed type and max size
Code:
$this->load->library('upload');
$config['upload_path'] = 'admin/upload/';
$config['allowed_types'] = 'jpg|jpeg';
$config['max_size'] = '100';
$this->load->library('upload', $config);
$this->upload->initialize($config);
$this->upload->set_allowed_types('*');
if (!$this->upload->do_upload('user_image')){
$data = array('msg' => $this->upload->display_errors());
$flag="1";
}else {
$image_path = $this->upload->data();
$flag="2";
}
Output:
$flag always set to 2...even I uploaded file .png or .gif and same problem for max_size
Try increasing the max_size. Also remove the line $this->upload->set_allowed_types('*');

Can't upload ogg or ogv file

I can't upload ogg / ogv file. the error message is
The filetype you are attempting to upload is not allowed.
I have tried this in the mimes.php
'ogg' => array('application/x-ogg', 'application/ogg', 'audio/x-ogg, application/octet-stream'),
'ogv' => array('application/ogv', 'video/ogv')
and in the controller
$config['upload_path'] = 'assets/assets/video/';
$config['allowed_types'] = 'ogv|ogg';
$config['file_name'] = "samsame";
$config['remove_spaces'] = TRUE;
$this->load->library('upload', $config);
$this->upload->do_upload('videoname');
$error = $this->upload->display_errors();
You have to initialize the $config array, so your problem will be solved. You are trying to upload without initializing. Please write the below line after loading the library.
$this->upload->initialize($config);
Also check that your form must be multipart
You can also rewrite the $config array if above solution dose't work. Here sample_simage
is the name of input type.
$allowed_types = 'ogg|ogv';
$config['upload_path'] = './assets/uploads/';
$config['allowed_types'] = substr($allowed_types, strpos($allowed_types,substr($_FILES['sample_simage']['name'], -3)), 3);
$this->load->library('upload', $config);
$this->upload->do_upload('sample_simage');

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.

Categories