Upload ZIP-files with CodeIgniter? - php

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.

Related

In Codeigniter How to check if the uploaded file is actually a pdf or jpg or png?

In Codeigniter How to check if the uploaded file is actually a pdf or jpg or png? Because, if we upload an .exe file with .pdf extension then also it gets uploaded without any problem. So, it there a proper way to actually check the file and its content to be able to determine whether it is actually a pdf or exe. Because with just the file extension anything can be uploaded. Please, help me find a proper solution for this. Is there any native php function through which we can achieve this. If so a sample code might be helpful.
you can use mime_content_type() function which is in built in php it provides actual content type even if the extension is changed
php docs
<?php
echo mime_content_type('abcd.pdf') //application/pdf
?>
checking mime_content_type while uploading
$mimetype = mime_content_type($_FILES['file']['tmp_name']);
if(in_array($mimetype, array('image/jpeg', 'image/gif', 'image/png'))) {
move_uploaded_file($_FILES['file']['tmp_name'], '/whatever/something/imagedir/' . $_FILES['file']['name']);
echo 'OK';
} else {
echo 'It is not an image';
}
You need to check real file type and given file type like this:
$config['upload_path'] = './uploads/';
$config['allowed_types'] = 'gif|jpg|png|pdf';
$this->load->library('upload', $config);
$file = $_FILES['userfile'];
// given file type
$gftype=pathinfo($file['name'], PATHINFO_EXTENSION);;
// real file type
$rftype = explode('/',mime_content_type($file['tmp_name']))[1];
if($gftype === $rftype){
if (! $this->upload->do_upload('userfile')){
echo "Error";
}else{
echo "Success";
}
}else{
echo 'This is not real extension';
}

Codeigniter: The filetype you are attempting to upload is not allowed

I'm using CI latest version. Got error while uploading a JPG files. I use the code from here https://www.codeigniter.com/userguide3/libraries/file_uploading.html and a little bit from Multiple files upload in Codeigniter
The Controller:
$config = array(
'upload_path' => 'path to upload',
'allowed_types' => 'jpg|gif|png|jpeg',
'overwrite' => 0,
'max_size' => 1024,
);
$this->load->library('upload', $config);
$this->upload->initialize($config); // Make sure it has been initialized
if (!$this->upload->do_upload('gambar1')){
$error = array('error' => $this->upload->display_errors());
return $error;
}else{ echo 'success'; }
The View:
<?php echo form_open(base_url().'report/send', 'method="post" enctype="multipart/form-data"', $hidden);?>
<input type="file" class="hidden" name="gambar1"></form>
When I'm trying to upload JPG files, it gives me The filetype you are attempting to upload is not allowed.
Any idea?
This might help
'allowed_types' => 'jpg|gif|png|jpeg|JPG|PNG',
print errors and see what it returns after this
SOLUTION:
I've try to upload it to server. But it still gives me the same error. Then i found this: Codeigniter : The filetype you are attempting to upload is not allowed. Yesterday it was fine
"The most recent time I have had this is when I upgraded my core Codeiginter version from 2.x to 3.x. I updated the contents of the system directory, but not application/config. I didn't spot the fact that the application/config/mimes.php file is slightly different - the newer version returns an array whereas the old one included it as a var. The fix was to copy in the newer version of mimes.php"
I had the same problem and I figured out that the error came from the uploaded pictures.
If they were originally .png files and I changed them myself to .jpg, I would get this kind of error. The same happened when changing the mime type of any file.
What I did was put back the original mime type and it worked just fine.
i am faced same problem.......and find too much solution but no any solution worked for me........then i was try to use different method to upload and that is worked......CI Version is 3.1.10
here is your solution
$report_pdf = "";
if (!empty($_FILES) && isset($_FILES["report_pdf"])) {
$ext = pathinfo($_FILES['report_pdf']['name'], PATHINFO_EXTENSION);
$new_name = 'report_pdf_' . time() . '_' . $this->get_random_string(4) . '.' . $ext;
$file_url = "uploads/" . $new_name;
$config['file_name'] = $new_name . "." . $ext;
$config['upload_path'] = "uploads/";
$config['allowed_types'] = '*';
$this->load->library('upload', $config);
$_FILES['userfile2']['name'] = $new_name . "." . $ext; //$_FILES['songs']['name'][$i];
$_FILES['userfile2']['type'] = $_FILES['report_pdf']['type'];
$_FILES['userfile2']['tmp_name'] = $_FILES['report_pdf']['tmp_name'];
$_FILES['userfile2']['error'] = $_FILES['report_pdf']['error'];
$_FILES['userfile2']['size'] = $_FILES['report_pdf']['size'];
$this->upload->initialize($config);
if ($this->upload->do_upload('userfile2')) {
$finfo = $this->upload->data();
$report_pdf = $file_url;
} else {
$error = $this->upload->display_errors();
$result = array(
'msg' => strip_tags($error)
);
}
}
For PDF, CI3 mimes array don't have "application/octet-stream" in the config/mimes.php file. Add "application/octet-stream" in the pdf key. Works fine.

PHP: How to get File Name & Extension of uploaded file in Codeigniter?

i need to store only filename + extension of file when upload and rename done to my databse.my problem is can't get extension.
$new_file_name=date("mdY")."_".time();
$config['upload_path'] = './assets/images/';
$config['allowed_types'] = 'gif|jpg|png';
$config['max_size'] = 2048;
$config['max_width'] = 1024;
$config['max_height'] = 768;
$config['file_name'] = $new_file_name;
$this->load->library('upload', $config);
if ( ! $this->upload->do_upload('uploadFile'))
{
$error = array('error' => $this->upload->display_errors());
var_dump($error);
}
else
{
$data = array('upload_data' => $this->upload->data());
var_dump($data);
echo $config['file_name'] . $config['file_ext'];
}
my next question load helper form for ? or need load form to use library form_validation ?
$this->load->helper(array('form')); for ?
$this->load->library('form_validation'); this for validation rule
custom upload filename codeigniter
first you can to explode with (.) in filename, then you get array, then catch the end array with function end().
and last i can to join filename with this date.
$dname = explode(".", $_FILES['gambar']['name']);
$ext = end($dname);
$_FILES['gambar']['name'] = strtolower('fitur_h_'.date('YmdHis').'.'.$ext);
//fitur_h_20200120143157.png //output filename
i hope my answer can help you
After you uploaded the file, you can get its property by:
$saved_file_name = $this->upload->data('file_name');
// will give you the filename along with the extension
If you want to get the file extension before uploading it, use core php:
$file_ext = pathinfo($_FILES["file"]["name"], PATHINFO_EXTENSION);
or to make it clean
$filename= $_FILES["file"]["name"];
$file_ext = pathinfo($filename,PATHINFO_EXTENSION);
Simply use the below code for image name with extension on post to solve issue,
$config['file_name'].$this->upload->data('file_ext')
I was having the same problem and i just figured it out.
I was trying to get the name with this: $this->upload->data('file_name');
but it was always empty, and i just finded out that you need to set something before you can access the data() array.
When your code successfully uploads the file, type this: $data = array('upload_data' => $this->upload->data());
after that, all the data() indexes will be accessible, 'file_name' included. The list with all indexes is in the documentation: https://codeigniter.com/user_guide/libraries/file_uploading.html#preferences
ps: I know it's been 2 years since this post but maybe someone can find it helpful if they stuck with this too.
I know this is too late, still I'm giving the answer.
If you want to get the image extension, then use pathinfo function with PATHINFO_EXTENSION parameter.
eg.
$imageExtention = pathinfo($_FILES["image"]["name"], PATHINFO_EXTENSION);
echo imageExtention;
you can get file name using this code
$file_name = $data['file_name'];
echo $file_name;
and extension for file
$ext = implode('.',$file_name);
echo $ext[1];

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