0

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.

saudade
  • 1
  • 3
  • Is this on apache? what is your .htaccess look like? also it could be because php cannot parse .rtf https://stackoverflow.com/questions/678192/is-it-possible-to-display-an-rtf-file-inside-a-web-page-using-php – exception_thrown Apr 01 '19 at 20:30
  • thank you for the answer, i read the page, but i'm not looking to parse the file content, i just want to get the extension of the .rtf file and put this in the variable as it has worked with the other file types. – saudade Apr 01 '19 at 20:37
  • is this on apache? do you have a .htaccess file? – exception_thrown Apr 01 '19 at 20:44
  • No, this is not in Apache, it's windows server – saudade Apr 01 '19 at 20:46
  • Windows Server is a platform and doesn't know what PHP is. Do you perhaps mean that you are using IIS which is the MS flavor of web server? – Dave Apr 01 '19 at 23:28

1 Answers1

0

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.

saudade
  • 1
  • 3