Загрузка изображений не работает правильно с PHP 7.2 + - PullRequest
0 голосов
/ 21 апреля 2020

Я создаю HTML / PHP форму с полем загрузки файла. Я собирал его с PHP 7.1, и все работало как положено, но когда я попытался запустить более новую версию PHP, некоторые файлы изображений не загружались должным образом. Из типов, которые я тестировал HEI C и PNG, кажется, не обрабатываются правильно в PHP 7.2 или выше.

Код:

function get_mime_type($file) {
    $mtype = false;
    if (function_exists('finfo_open')) {
        $finfo = finfo_open(FILEINFO_MIME_TYPE);
        $mtype = finfo_file($finfo, $file);
        finfo_close($finfo);
    } elseif (function_exists('mime_content_type')) {
        $mtype = mime_content_type($file);
    } 
    return $mtype;
}

    //Attachment Checks
    if (!empty($_FILES['attachment'])) { //If there are attachments
        $count = count($_FILES['attachment']['name']);
        if ($count > 0) {
            for ($i = 0; $i < $count; $i ++) {

                //File Format Check
                $imageFileType = strtolower(pathinfo($_FILES["attachment"]["name"][$i],PATHINFO_EXTENSION));
                $imageMIME = get_mime_type($_FILES["attachment"]["tmp_name"][$i]);
                if (!in_array($imageFileType, $validExtensions)){
                    $fileFormatError = 1;
                }
                if (!in_array($imageMIME, $validMIMETypes)){
                    $fileFormatError = 1;
                }
                echo $imageMIME . " ";
            }
        }

        //Checks
        if (array_sum($_FILES["attachment"]["size"]) > ($totalFileSizeLimit * 1048576)){
            $fileSizeError = 1;
        }
    }
echo ''; print_r($_FILES['attachment']); echo '
';

Выход с PHP 7.1:

application/octet-stream image/png image/jpeg application/pdf
Array
(
    [name] => Array
        (
            [0] => IMG_1209.HEIC
            [1] => vlcsnap-2018-09-14-09h25m12s582.png
            [2] => IMG_0919.JPG
            [3] => Notifications Report.pdf
        )

    [type] => Array
        (
            [0] => application/octet-stream
            [1] => image/png
            [2] => image/jpeg
            [3] => application/pdf
        )

    [tmp_name] => Array
        (
            [0] => /tmp/phpELiNGi
            [1] => /tmp/phpUQzMvA
            [2] => /tmp/phpTsoccW
            [3] => /tmp/php4u2G9k
        )

    [error] => Array
        (
            [0] => 0
            [1] => 0
            [2] => 0
            [3] => 0
        )

    [size] => Array
        (
            [0] => 2896766
            [1] => 2681160
            [2] => 2066662
            [3] => 79876
        )

)

Выход с PHP 7.2 или выше:

image/jpeg application/pdf
Array
(
    [name] => Array
        (
            [0] => IMG_1209.HEIC
            [1] => vlcsnap-2018-09-14-09h25m12s582.png
            [2] => IMG_0919.JPG
            [3] => Notifications Report.pdf
        )

    [type] => Array
        (
            [0] => 
            [1] => 
            [2] => image/jpeg
            [3] => application/pdf
        )

    [tmp_name] => Array
        (
            [0] => 
            [1] => 
            [2] => /tmp/phpHO95fL
            [3] => /tmp/phpeGaIre
        )

    [error] => Array
        (
            [0] => 1
            [1] => 1
            [2] => 0
            [3] => 0
        )

    [size] => Array
        (
            [0] => 0
            [1] => 0
            [2] => 2066662
            [3] => 79876
        )

)
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...