How do I check whether the php_fileinfo.dll extension is active or not via php code?

Asked

Viewed 1,284 times

3

I would need to run this code on another machine, in case the extension:

extension=php_fileinfo.dll

I need it to use the mimetype, if disabled I would use another code without using mimetype to make the extension comparison.

  • Do you want to check or activate?

  • @Guilhermenascimento At first I wanted to perform the verification, because I do not know it is possible to perform the activation via code, so it could be both.

2 answers

2


Beyond the extension_loaded quoted you can use function_exits to check if the function is available (it would be like a "Feature Detection"), you can do so in the script:

if (!function_exits('finfo_file')) {
   echo 'Extensão fileinfo não disponível, habilite no php.ini';
   exit;
}

So you check if the function exists.

Note that using $_FILES['nome']['type'] is can cause problems because in some atypical situations it may return unexpected results, for example you have a file with the extension .jpg, but the data is a .txt, when you upload the [type] returns something like:

array(5) {
  ["name"]=>
  string(9) "teste.jpg"
  ["type"]=>
  string(10) "image/jpeg"
  ["tmp_name"]=>
  string(19) "Z:\.tmp\php4EA4.tmp"
  ["error"]=>
  int(0)
  ["size"]=>
  int(3)
}

But in fact the teste.jpg has only this in the content a b c, so when to use things like imagecreatefromjpeg will have problems, I know it seems unlikely to have a file that is not image but with the extension .jpg, but there are many cases where the file is a webp or png, but the customer dropped as jpeg, so if it were to upload this could confuse your script, there is also the possibility of the user to download a photo and it will be truncated but it does not take account and will then go up to your server.

Applications made in Flash also usually send the type as application/octet-stream in requisitions as cited by Compare File Extension:

... I’ve seen people say otherwise, and I’ve seen cases where mime-type comes wrong or useless (application/octet-stream, which can be anything).

It is not always possible to enable the extension, but on some servers it is possible to use a function called dl();, but still you can try:

function carregarExtensao($nome)
{
    if (!extension_loaded($nome)) {
        if (strtoupper(substr(PHP_OS, 0, 3)) === 'WIN') {
            //Carrega em Windows
            return dl('php_' . $nome . '.dll');
        } else {
            //Carrega em unix-like
            return dl($nome . '.so');
        }
    }

    return true;
}

if (!carregarExtensao('fileinfo')) {
     echo 'Não foi possivel carregar a extensão';
     exit;
}

NOTE: This function was removed in most Sapis in PHP5.3.0 and was removed in PHP-FPM in PHP7 version

If it does not work on your server, then it is not possible.

  • I understood Guilherme very enlightening your explanation, but there is the possibility that if you are disabled the extension,my code enable this extension to be able to run mimetype quietly ?

  • @Caleb so I asked you if it was check or enable rs, so there’s no way to activate at runtime unfortunately. There is even a function I will edit the answer and put an example.

  • 1

    If Extension=php_fileinfo.dll were disabled I wanted to run another extension comparison without using mimetype, but with vc said the server is very vulnerable to receiving improper arqruivos. Best to only perform the check !

  • @Many extensions are disabled on servers to improve performance, so we only enable what we need to use. Today it is so common (or better necessary) the use of fileinfo, really it would be interesting if it were already enabled, who knows in php7 =)

1

Checking whether the extension has been loaded

if (!extension_loaded('fileinfo')) {
    // a extensão fileinfo não está carregada
} else {
    // a extensão fileinfo foi carregada
}

http://php.net/extension_loaded

The global variable $_FILES

If you wanted to get the mime type at the time of an upload, you can find the information directly in the global variable $_FILES.

Example

$_FILES['nome_do_campo']['type']

In this case it does not need a specific extension like the Fileinfo and is more secure than "appeal" to the extension of the file nomenclature.

However, do not trust 100% a single parameter. $_FILES rescues client-provided information (browser for example).

The native function getimagesize()

In PHP, there is the native function getimagesize() where you can get information about the archive.

$size = getimagesize($filename);
echo $size['mime'] // aqui o mime-type.

Suggested implementation

An example of how you can implement a routine that reads the file type:

$path = '/local/do/arquivo.jpg';

if (!extension_loaded('fileinfo')) {
    /*
    Obtendo informação de getimagesize()
    */
    $size = getimagesize($path);
    $mime_type = $size['mime']; // aqui o mime-type.
    unset($size);
} else {
    /*
    Obtendo informação de Fileinfo
    */
    $mime_type = finfo_open(FILEINFO_MIME, $path);
}

'O tipo do arquivo é: '.$mime_type;

Browser other questions tagged

You are not signed in. Login or sign up in order to post.