Uploading in Codeigniter - The filetype you are attempting to upload is not allowed
I am getting the error: The filetype you are attempting to upload is not allowed when I try to uplaod any file.
if(!empty($_FILES['proof_of_purchase']['name'])) {
开发者_运维知识库 $config['upload_path'] = './uploads/invoices/';
$config['allowed_types'] = 'gif|jpg|jpeg|png|pdf|bmp';
$config['max_size'] = '3000';
$this->load->library('upload', $config);
// if there was an error, return and display it
if (!$this->upload->do_upload('proof_of_purchase'))
{
$data['error'] = $this->upload->display_errors();
$data['include'] = 'pages/classic-register';
} else {
$data['upload_data'] = $this->upload->data();
$filename = $data['upload_data']['file_name'];
}
}
I have tried many different files- mostly gif & jpeg and get the same error each time.
var_dump($_FILES); gives me:
array(1) { ["proof_of_purchase"]=> array(5) { ["name"]=> string(28) "2010-12-04_00019.jpg" ["type"]=> string(10) "image/jpeg" ["tmp_name"]=> string(19) "D:\temp\php2BAE.tmp" ["error"]=> int(0) ["size"]=> int(58054) } }
I have checked the mime config and it contains the right stuff. Example:
'jpeg' => array('image/jpeg', 'image/pjpeg'),
'jpg' => array('image/jpeg', 'image/pjpeg'),
'jpe' => array('image/jpeg', 'image/pjpeg'),
If you're using Codeigniter version 2.1.0 there is a bug in the Upload library. See http://codeigniter.com/forums/viewthread/204725/ for more details.
Basically what I did was modify a few lines of code in the File Upload Class (Location: ./system/libraries/Upload.php)
1) modify Line number 1044
from:
$this->file_type = @mime_content_type($file['tmp_name']);
return;
to this:
$this->file_type = @mime_content_type($file['tmp_name']);
if (strlen($this->file_type) > 0) return;
2) modify line number 1058
from:
@exec('file --brief --mime-type ' . escapeshellarg($file['tmp_path']), $output, $return_code);
to this:
@exec('file --brief --mime-type ' . escapeshellarg($file['tmp_name']), $output, $return_code);
As you can probably see, line 1058 tries to use an array value that does not exist.
I've had these same problems with CI and haven't been able to find a fix on the forums or via google. What I've done is to allow all filetypes, so that the file gets uploaded. Then, I handle the logic manually to determine whether to allow/keep the file, or delete it and tell the user that filetype is not allowed.
$config['upload_path'] = './uploads/invoices/';
$config['allowed_types'] = '*'; // add the asterisk instead of extensions
$config['max_size'] = '3000';
$this->load->library('upload', $config);
if (!$this->upload->do_upload('proof_of_purchase'))
{
$data['error'] = $this->upload->display_errors();
$data['include'] = 'pages/classic-register';
} else {
$data['upload_data'] = $this->upload->data();
// use custom function to determine if filetype is allowed
if (allow_file_type($data['upload_data']['file_ext']))
{
$filename = $data['upload_data']['file_name'];
}
else
{
show_error('File type is not allowed!');
}
}
EDIT - This is assuming you're using CI 2 (in CI 1 you can follow the tutorial here to allow all filetypes: http://net.tutsplus.com/tutorials/php/6-codeigniter-hacks-for-the-masters/)
What I did was create my own Library "MY_Upload" to extend the CI_Upload Class, then I just copied the CI_Upload class and applied the changes outlined by Adam (thanks a bunch BTW for the solution) in my custom library.
This allows me to use the standard CI syntax, and avoid hacking the original files! My library is automatically used because it simply "extends" the original, it's a completely painless solution and won't break if for some reason you have to replace the original files.
PS: I do this with the Logging class also for when I want to generate custom logs.
I had the same issue. You may need to check if the application recognizes the mimetype of the file that is being uploaded. Adding a new mimetype to config/mimes.php fixed the issue. :-)
This is for Codeigniter version 2.2. If this question is still relevant. I traced the fault in file system/libraries/upload.php file to function: protected function _file_mime_type($file)
at line 1032 and line 1046: $this->file_type = $matches[1];
When I was uploading a file with extension .txt the statement at line 1046 seems to assign an incorrect value of 'text/x-asm'
to $this->file_type
which is later compared to 'text/plain'
and since mime types do not match the test fails and signals an inappropriate file type and error message:
'The filetype you are attempting to upload is not allowed'
Solution, not sure, but quick fix that appears to work, change condition of line 1032 to NOT
so that it reads: if (!function_exists('finfo_file'))
instead of: if (function_exists('finfo_file'))
. Hope this can help someone.
1) Allow all filetypes. 2) Manually set validation rule with traditional php to accept or reject file types. 3) if validation rules are obeyed, then upload using CI upload helper.
if (isset($_FILES['upload_file']) && !empty($_FILES['upload_file'] ['name'])) {
$file_name=$_FILES['upload_file'] ['name'];
$acceptedext=array("zip","pdf","png","jpg","jpeg");
$a=(explode('.', $file_name));
$b=end($a);
$file_ext=strtolower($b);
if (!in_array($file_ext, $acceptedext)) {
$this->session->set_flashdata( 'flash_message_error', "file format not accepted!" );
redirect( base_url( 'page' ) );
}
else{
$config['upload_path'] = './uploads/';
$config['allowed_types'] = '*';
$this->load->library('upload');
$this->upload->initialize($config);
if (!$this->upload->do_upload('upload_file')) {
$error = $this->upload->display_errors('', ' ');
$this->session->set_flashdata( 'flash_message_error', $error );
redirect( base_url( 'page' ) );
} } }
The solution is replace the Upload.php in the system/libraries/ by Upload.php of CodeIgniter v2.0.3
This problem is caused by not having the PHP FileInfo extension. The function the upload class uses is part of that extension.
http://www.php.net/manual/en/ref.fileinfo.php
If you don't want to change system files. Just try this:
Open
system/libraries/Upload.php
(methoddo_upload()
line 205) and do thisvar_dump($this->file_type); exit();
Try to upload some file.
Add file type, which you see in
var_dump()
toapplication/config/mimes.php
.
Little example: you have problem with .docx
. Try to add:
'docx' => array('application/zip', 'application/msword', 'application/vnd.openxmlformats-officedocument.wordprocessingml.document')
To application/config/mimes.php
.
It's probably worth checking that you have the latest version of the application/config/mimes.php as instead of setting a variable $mimes it now simply returns an array.
new mimes.php
return array
old mimes.php
$mimes = array
If you still have the old version of mimes.php the Common.php function &get_mimes() returns an empty array. This has the knockon effect of breaking Upload.php
Once I traced this, all was working fine :)
The same problem I had with, when I was trying to do an Upload form to upload, ".xlsx" files, which in CodeIgniter's mimes.php file has an entry in it's array, to represent "xlsx" file extensions.
So here in the following link, I have described what it really takes to get through this problem and figure out the solution!
精彩评论