问题描述
我正在尝试使用"extension X mime"
文件值通过phpmailer创建上传文件的交叉验证,系统正在处理几乎所有需要执行此操作的文件,但是与.rtf
文件不同的是,脚本是工作.
I am trying to create a cross validation of uploaded files through phpmailer using the "extension X mime"
file values, the system is working to almost all files that i need to do this, but exceptionally with .rtf
files the script isn't working.
扩展名消失了,如果文件为.rtf
,则脚本无法获取该扩展名.我正在使用此脚本: https://stackoverflow.com/a/33349901/4623271 进行了一些修改.
The extension just disappear, the script can't get this if the file is .rtf
. I'm using this script: https://stackoverflow.com/a/33349901/4623271 with some adaptations.
是变量$ext
,在其中我可以获取验证允许的任何文件的扩展名,除了.rtf
文件,当文件为.rtf
时,该变量显然为空.
Bellow in the code, is the variable $ext
where i can get the extension of any files allowed by the verification, except .rtf
files, when the file is a .rtf
, the variable becomes apparently empty.
我尝试使用$ext = end(explode(".", $_FILES["uploadedFile"]["name"]));
,但是以相同的方式,当.rtf
文件为时,变量变为空.
I tried using $ext = end(explode(".", $_FILES["uploadedFile"]["name"]));
but in the same way, when is a .rtf
file, the variable becomes empty.
// mime verification
$finfo = new finfo(FILEINFO_MIME_TYPE);
if (false === $ext = array_search(
$finfo->file($_FILES['uploadedFile']['tmp_name']),
array(
'doc' => 'application/msword',
'docx' => 'application/vnd.openxmlformats-officedocument.wordprocessingml.document',
'rtf' => 'application/msword',
'odt' => 'application/vnd.oasis.opendocument.text',
'txt' => 'text/plain',
'pdf' => 'application/pdf',
),
true
)) {
$errorMsg .= "<br> Please, watch that the formats allowed are: \"doc\", \"docx\", \"rtf\", \"odt\", \"txt\", \"pdf\"' ";
}
谢谢您的宝贵时间.
推荐答案
我发现问题出在我在数组中用于验证.rtf
文件的mime值中.
I have discovered that the problem is in mime value that i was using in array to validate the .rtf
file.
$finfo = new finfo(FILEINFO_MIME_TYPE);
读取的mime类型不是在联机表中找到的具有此类信息的常见mime类型,并且我已经使用它来创建初始数组.
The mime type readed by $finfo = new finfo(FILEINFO_MIME_TYPE);
is not the usual mime type finded in online tables with this kind of information and that i have used to create the initial array.
在一个巴西PHP Telegram组中寻求帮助之后,我得到了提示提示,以分析$finfo
变量的值.
After searching for help in a Brazilian PHP Telegram group, I received the hint to analyse the value of the $finfo
variable.
当我应用var_dump ($finfo->file($_FILES['uploadedFile']['tmp_name']));
时,我发现FILEINFO_MIME_TYPE
文件的mime是FILEINFO_MIME_TYPE
而不是application/rtf
,这是我上面说的text/rtf
而不是application/rtf
,这是.rtf
文件.
When i applied var_dump ($finfo->file($_FILES['uploadedFile']['tmp_name']));
I discovered that to FILEINFO_MIME_TYPE
the mime of .rtf
files is: text/rtf
and not application/rtf
, that as i said above, is the most common option of mime type to .rtf
files.
因此,由于脚本期望text/rtf
与.rtf
文件相关联,因此发生了验证错误.
Because of this, the validation error was occurring since the script expected text/rtf
to associate with .rtf
file.
在更改text/rtf
而不是application/msword
或application/rtf
的键值之后,脚本按预期工作.
After I change the key value for text/rtf
instead application/msword
or application/rtf
, the script worked as expected.
现在,我正在使用phpmailer发送带有mime验证的附件.
Now i´m sending attachments with mime validation using phpmailer.
感谢所有尝试以某种方式提供帮助的人.
Thanks to all who tried to help in some way.
这篇关于"rtf"在使用"finfo"执行MIME/扩展名验证脚本的过程中,文件扩展名消失了. PHP类的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!