How to check if the php_fileinfo.dll extension is active or not via php code?

3

I would need to run this code on another machine if the extension:

extension=php_fileinfo.dll

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

    
asked by anonymous 22.03.2016 / 20:07

2 answers

2

In addition to the extension_loaded 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'] can cause problems because in some atypical situations it may return unexpected results, for example you have a file with the .jpg extension, but the data is .txt , when you upload [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 actually teste.jpg has only this in content a b c , so when using things like imagecreatefromjpeg will have problems, I know it seems unlikely to have a file that is not image but with the .jpg extension %, but there are a lot of cases where the file is a webp or png , but the client has downloaded as jpeg , so if it uploads it may confuse your script, there is also the possibility of the user downloading a photo and it will be truncated but it will not be noticed and will then upload to your server.

Applications made in Flash also usually send the type as application/octet-stream in requests as quoted by Compare file extension :

  

... I've seen people saying the opposite, and I've seen cases where the 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.

    
22.03.2016 / 20:14
1

Checking if extension has been loaded

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

link

The global variable $_FILES

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

Example

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

In this case you do not need a specific extension such as Fileinfo and it is safer than "appealing" to the file naming extension.

However, do not rely 100% on a single parameter. The $ _FILES variable retrieves information provided by the client (browser for example).

The native function getimagesize()

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

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

Suggestion for 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;
    
22.03.2016 / 20:11