Uploading svg file in Codeigniter - php

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

Related

Problem with mimes xlsx and xls codeigniter

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

Why can't I upload zip type file?

I successfully create multiple file upload. But, for some reason it fails to upload zip type files.
$config['upload_path'] = FCPATH."assets/uploads/";
$path = $config['upload_path'];
$config['allowed_types'] = 'gif|jpg|jpeg|png|bmp|doc|docx|ppt|pptx|xls|xlsx|txt|pdf|sql|html|rar|zip|7z';
$config['max_size'] = '5120';
$this->load->library('upload', $config);
$this->upload->initialize($config);
If I add this $this->upload->set_allowed_types('*');. The zip files are uploaded. But, I don't want to use this. Because it allows all type to be uploaded.

Can not upload doc or docx file in codeigniter

I am working in codeigniter framework.I want to upload doc or docx file.So i have write code like this:
$config['upload_path'] = './uploads/resume/';
$config['allowed_types'] = 'docx|doc|DOC|DOCX';
$this->upload->initialize($config);
if ( ! $this->upload->do_upload('resume'))
{
print_r($error = array('error' => $this->upload->display_errors()));
}
else
{
$upload = $this->upload->data();
print_r($upload);exit;
}
Here, it shows and error like The filetype you are attempting to upload is not allowed. And file is not uploaded.
I have also modify mimes.php file and add
'doc' => array('application/msword', 'application/vnd.ms-office'),
'docx' => array('application/vnd.openxmlformats-officedocument.wordprocessingml.document', 'application/zip', 'application/msword', 'application/x-zip'),
When I print $this->upload->data() then it shows file_type as application/vnd.oasis.opendocument.text
But, its not working.So how can I upload doc or docx file?
Here I have created doc array in mimes.php like this :
'doc' => array('application/msword', 'application/vnd.ms-office','application/vnd.oasis.opendocument.text')
and its working.
I don't see "application/vnd.oasis.opendocument.text" in your array of allowed mime types.
Don't forget max_size and allowed type to txt
$config['allowed_types'] = 'txt|doc|docx';
$config['max_size'] = 10000;
$this->load->library('upload', $config);
If still fails load OpenOffice extension too
$config['allowed_types'] = 'application/vnd.oasis.opendocument.spreadsheet | application/vnd.oasis.opendocument.text | application/vnd.oasis.opendocument.presentation |
application/vnd.openxmlformats-officedocument.wordprocessingml.document | application/vnd.ms-excel | application/vnd.openxmlformats-officedocument.presentationml.presentation | txt';
Add the following to the mimes.php file
'binary/octet-stream'
for each office document type you want to support. Not an ideal solution but works for me.

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

Upload ZIP-files with CodeIgniter?

I am using the following code to upload and extract a ZIP-archive in PHP using CodeIgniter.
$config['upload_path'] = 'backups/';
$config['allowed_types'] = 'zip';
$config['file_name'] = $project_id;
$this->load->library('upload', $config);
if ( file_exists('backups/' . $project_id . '.zip') ) {
try {
delete_file('backups/' . $project_id . '.zip');
} catch (exception $e) {
// do nothing...
}
}
if ( $this->upload->do_upload('zip_archive') ) {
$upload_data = $this->upload->data();
$file_name = 'backups/'.$upload_data['file_name'];
$this->sync_model->extract_zip_archive($project_id, $file_name, TRUE);
echo "Success";
} else {
echo $this->upload->display_errors('<p>','</p>');
}
I get the error though "The filetype you are attempting to upload is not allowed." even though it is a valid .zip file I am uploading. (It's size does not exceed the maximum allowed)
This problem also occurs with every filetype I've tried that's not an image.
Any pointers?
Cheers!
$config['allowed_type']
Should be a list of mime types, but it just so happens that sometimes just the file extension will work (this is the case with images at least).
Try:
$config['allowed_type'] = 'application/zip';
Although these are also mime types for zip:
application/x-zip
application/x-zip-compressed
application/octet-stream
application/x-compress
application/x-compressed
multipart/x-zip
I solved it using the code provided by "kofic" here: http://ellislab.com/forums/viewthread/113029/
You can keep your 'allowed_types' as 'zip', but making sure that you extend the mime types that are considered 'zip'.
I changed line 54 of my 'application/config/mimes.php' to:
'zip' => array(
'application/x-zip',
'application/zip',
'application/x-zip-compressed',
'application/octet-stream',
'application/x-compress',
'application/x-compressed',
'multipart/x-zip'),
(Credit to Matthew Rapati for the list of MIME types)
The truth is that once you allow 'application/octet-stream', the uploaded file could be pretty much anything so part of the purpose of restricting the allowed types vanishes. But since CI forces you to set it to something, I think it is still better than:
$config['allowed_types'] = '*';
Which also works.

Categories