Problem with mimes xlsx and xls codeigniter - php

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

Related

Codeigniter 2 File Upload Not Working / Possible Folder Permission Issue

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.

CodeIgniter FileUpload Not Allowed

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

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

Uploading svg file in Codeigniter

I am trying to upload file with type svg in Codeigniter. But i could not do it with codeigniters built in upload library. I have tried to do it by adding this line in congig > mimes.php file
'svgz' => 'image/svg+xml',
'svg' => 'image/svg+xml',
but still no solution. I am getting this error -
The filetype you are attempting to upload is not allowed.
here is my code
$dir = 'uploads/svg/';
$this->load->library('upload');
$config['file_name'] = $result . '.svg';
$config['upload_path'] = $dir;
$config['allowed_types'] = 'image/svg+xml';
$this->load->library('upload');
$this->upload->initialize($config);
$this->upload->do_upload();
Can anyone help me please?
thanks in advance
First of all, check the mime type of your image here
Then,
Open mimes.php from the config folder
then find the array element of index same as your image extension from the array,
add the mime type of your image to the array
In my case, I added 'image/svg':
'svg' => array('image/svg+xml', 'application/xml', 'text/xml', 'image/svg')
You need to put a list of allowed file extensions in $config['allowed_types'], separated by |, not MIME type. Try this -
$config['allowed_types'] = 'svg';
$config['allowed_types'] = 'image/svg+xml';
This should be:
$config['allowed_types'] = 'svg';

Can't upload Image CodeIgniter, filetype error

I have write below code :
function upld_logo()
{
$config['upload_path'] = './images/';
$config['allowed_types'] = 'gif|jpg|png';
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload())
{
$error = array('error' => $this->upload->display_errors());
$data['msg']='Sorry! Cant Upload' . $error['error'];
$data['main_content']='message';
$this->load->view('template',$data);
}
else
{
//$data = array('upload_data' => $this->upload->data());
$names = $this->upload->data();
$k=$names['file_name'];
$data['msg']='Your Logo Successfully Uploaded. ';
$data['main_content']='message';
$this->load->view('template',$data);
}
}
When I upload Image file with name logo.gif it show error message :
The filetype you are attempting to upload is not allowed.
Find this code in your system->libraries->Upload.php line 200
$this->file_type = preg_replace("/^(.+?);.*$/", "\\1", $this->file_type);
and replace with this:
$this->file_type = preg_replace("/^(.+?);.*$/", "\\1", $_FILES[$field]['type']);
Yes I also got the same error.
And if u change the $[allowed_types] = "*" (allowed all files)
you will find that upload is success but your image file don't have a type!
I think there's something wrong with the CI library maybe?
I have spent some time with the same issue. You might find codeigniters default upload library is using a couple of deprecated PHP functions to find the files mime type.... Good luck, I'm still trying to patch it myself .

Categories