php 在 Codeigniter 中上传 - 不允许您尝试上传的文件类型
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7495407/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Uploading in Codeigniter - The filetype you are attempting to upload is not allowed
提问by dangermark
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.
我尝试了许多不同的文件——主要是 gif 和 jpeg,每次都得到相同的错误。
var_dump($_FILES); gives me:
var_dump($_FILES); 给我:
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:
我检查了 mime 配置,它包含正确的东西。例子:
'jpeg' => array('image/jpeg', 'image/pjpeg'),
'jpg' => array('image/jpeg', 'image/pjpeg'),
'jpe' => array('image/jpeg', 'image/pjpeg'),
I've spent far too long on this and it's driving me nuts! Any ideas would be extremely helpful.
我在这上面花了太长时间,这让我发疯!任何想法都会非常有帮助。
回答by Adam
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.
如果您使用的是 Codeigniter 2.1.0 版,则上传库中存在错误。有关更多详细信息,请参阅http://codeigniter.com/forums/viewthread/204725/。
Basically what I did was modify a few lines of code in the File Upload Class (Location: ./system/libraries/Upload.php)
基本上我所做的是修改文件上传类中的几行代码(位置:./system/libraries/Upload.php)
1) modify Line number 1044
1)修改行号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
2)修改行号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.
您可能会看到,第 1058 行尝试使用不存在的数组值。
回答by swatkins
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.
我在 CI 上遇到了同样的问题,并且无法在论坛或通过 google 找到修复程序。我所做的是允许所有文件类型,以便上传文件。然后,我手动处理逻辑以确定是允许/保留文件,还是删除它并告诉用户不允许文件类型。
$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/)
编辑- 这假设您使用的是 CI 2(在 CI 1 中,您可以按照此处的教程来允许所有文件类型:http: //net.tutsplus.com/tutorials/php/6-codeigniter-hacks-for-the-大师/)
回答by Petar Zivkovic
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.
我所做的是创建我自己的库“MY_Upload”来扩展 CI_Upload 类,然后我只是复制了 CI_Upload 类并在我的自定义库中应用了 Adam 概述的更改(顺便说一句,感谢解决方案)。
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.
这让我可以使用标准的 CI 语法,避免破解原始文件!我的库被自动使用,因为它只是“扩展”了原始文件,这是一个完全无痛的解决方案,如果由于某种原因您必须替换原始文件,它不会中断。
PS: I do this with the Logging class also for when I want to generate custom logs.
PS:当我想生成自定义日志时,我也使用 Logging 类执行此操作。
回答by abhisek
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. :-)
我遇到过同样的问题。您可能需要检查应用程序是否识别正在上传的文件的 MIME 类型。添加一个新的 mimetype 到 config/mimes.php 修复了这个问题。:-)
回答by Jason
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:
这适用于 Codeigniter 2.2 版。如果这个问题仍然相关。我跟踪文件系统/库/ upload.php的文件,功能故障:protected function _file_mime_type($file)
在1032行和行1046:$this->file_type = $matches[1];
当我在上传线与扩展名为.txt的声明文件1046似乎分配不正确的值的'text/x-asm'
,以$this->file_type
这就是后来的与'text/plain'
mime 类型不匹配相比,测试失败并发出不适当的文件类型和错误消息:
'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.
解决方案,不确定,但似乎有效的快速修复,将第 1032 行的条件更改NOT
为:if (!function_exists('finfo_file'))
而不是:if (function_exists('finfo_file'))
。希望这可以帮助某人。
回答by Randika Vishman
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.
我遇到了同样的问题,当我尝试上传上传表单时,“.xlsx”文件在 CodeIgniter 的 mimes.php 文件中有一个条目,用于表示“xlsx”文件扩展名。
So here in the following link, I have described what it really takes to get through this problem and figure out the solution!
所以在下面的链接中,我已经描述了解决这个问题并找出解决方案的真正需要!
Best Regards,
此致,
Randika
兰迪卡
回答by markBradford
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.
可能值得检查您是否拥有最新版本的 application/config/mimes.php,因为它现在只是返回一个数组,而不是设置变量 $mimes。
newmimes.php
新的mimes.php
return array
返回数组
oldmimes.php
旧mimes.php
$mimes = array
$mimes = 数组
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
如果您仍然使用旧版本的 mimes.php,Common.php 函数 &get_mimes() 将返回一个空数组。这具有破坏 Upload.php 的连锁效应
Once I traced this, all was working fine :)
一旦我追踪到这个,一切都很好:)
回答by Focus Classic
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.
1) 允许所有文件类型。2)用传统的php手动设置验证规则来接受或拒绝文件类型。3) 如果遵守验证规则,则使用 CI 上传助手上传。
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' ) );
} } }
回答by iwebprog
The solution is replace the Upload.php in the system/libraries/ by Upload.php of CodeIgniter v2.0.3
解决办法是用CodeIgniter v2.0.3的Upload.php替换system/libraries/中的Upload.php
回答by Andrew
This problem is caused by not having the PHP FileInfo extension. The function the upload class uses is part of that extension.
这个问题是由于没有 PHP FileInfo 扩展引起的。上传类使用的函数是该扩展的一部分。