The “rtf” file extension is disappearing during the execution of the mime/extension validation script using “finfo” PHP class

橙三吉。 提交于 2019-12-24 23:23:54

问题


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.

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.

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.

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\"' ";
    }

For the time you spent reading this, thank you.


回答1:


I have discovered that the problem is in mime value that i was using in array to validate the .rtf file.

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.

After searching for help in a Brazilian PHP Telegram group, I received the hint to analyse the value of the $finfo variable.

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.

Because of this, the validation error was occurring since the script expected text/rtf to associate with .rtffile.

After I change the key value for text/rtf instead application/mswordor application/rtf , the script worked as expected.

Now i´m sending attachments with mime validation using phpmailer.

Thanks to all who tried to help in some way.



来源:https://stackoverflow.com/questions/55463047/the-rtf-file-extension-is-disappearing-during-the-execution-of-the-mime-extens

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!