Acceptable upload – Eternal Night https://www.shuijingwanwq.com There is no problem not worth solving, and no technology not worth learning! Sun, 31 May 2026 07:21:31 +0000 en-US hourly 1 https://wordpress.org/?v=7.0 When uploading a file based on Yii 2.0, the list of files that can be uploaded for upload has been added: .jpg, but the solution is still not allowed to upload https://www.shuijingwanwq.com/en/2020/01/21/14986/ https://www.shuijingwanwq.com/en/2020/01/21/14986/#respond Tue, 21 Jan 2020 06:03:14 +0000 https://www.shuijingwanwq.com/?p=14986 浏览量: 15

1. List of file extensions that can be uploaded: ogg, pdf, xml, zip, gz, mp4, mp3, wav, webm, gif, jpeg, jpg, png, webp, SVG, SVGZ, TIFF, CSS, CSV, TXT, VCF, VCARD, MOV, QT, MKV, MK3D, MKA, MKS, WMV, FLV, DOC, docx, xls, xlsx, ppt, pptx

2. The upload extension is: .jpg, the upload failed, prompt: not allowed, as shown in Figure 1

上传扩展名为:.jpg 的文件,上传失败,提示:不允许

Figure 1


{
    "code": 226004,
    "message": "数据验证失败:只允许使用以下文件扩展名的文件:ogg, pdf, xml, zip, gz, mp4, mp3, wav, webm, gif, jpeg, jpg, png, webp, svg, svgz, tiff, css, csv, txt, vcf, vcard, mov, qt, mkv, mk3d, mka, mks, wmv, flv, doc, docx, xls, xlsx, ppt, pptx。"
}


 

Assign the uploadedFile instance array to upload::files, print it out


Array
(
    [0] => yii\web\UploadedFile Object
        (
            [name] => 20191204113827.jpg
            [tempName] => E:\phpuploadtmp\php6053.tmp
            [type] => image/jpeg
            [size] => 1970110
            [error] => 0
        )

)



4. Edit the file: /common/components/validators/filevalidator.php, modify the method: validateExtension($file) to start debugging, request again, output: 2


    /**
     * Checks if given uploaded file have correct type (extension) according current validator settings.
     * @param UploadedFile $file
     * @return bool
     * @throws InvalidConfigException when the `fileinfo` PHP extension is not installed and `$checkExtension` is `false`.
     */
    protected function validateExtension($file)
    {
        $extension = mb_strtolower($file->extension, 'UTF-8');

        if ($this->checkExtensionByMimeType) {
            $mimeType = FileHelper::getMimeType($file->tempName, null, false);
            if ($mimeType === null) {
                echo 1;
                exit;
                return false;
            }

            $extensionsByMimeType = FileHelper::getExtensionsByMimeType($mimeType);

            if (!in_array($extension, $extensionsByMimeType, true)) {
                // MS Office 2007 扩展(docx、xlsx),其 MIME 类型为 application/zip 的特殊处理
                $msMimeTypes = ['application/zip'];
                $msExtensions = ['docx', 'xlsx'];
                if (!(in_array($mimeType, $msMimeTypes) && in_array($extension, $msExtensions)))
                {
                    echo 2;
                    exit;
                    return false;
                }
            }
        }

        if (!in_array($extension, $this->extensions, true)) {
            echo 3;
            exit;
            return false;
        }

        return true;
    }


5. CheckExtensionByMimeType: Whether to judge the file extension by the mime type of the file. If the file extension determined by MIME is not the same as the extension of the given file, the file will be considered invalid. The default is true, which means that the above detection is performed. Print the output in sequence: $mimeType, $extensionsByMimeType, $extension, and the values are: as shown in Figure 2

checkExtensionByMimeType:是否通过文件的 MIME 类型来判断其文件扩展。若由 MIME 判定的文件扩展与给定文件的扩展不一样,则文件会被认为无效。默认为 true,代表执行上述检测。依次打印输出:$mimeType、$extensionsByMimeType、$extension,其值分别为:

Figure 2


image/png

Array
(
    [0] => png
)

jpg


6. Analysis results, since: $extensionsByMimeType and $extension do not match, it is normal to not allow uploading, which is normal. The extension is changed to png, and the upload is successful, as shown in Figure 3

分析结果,由于:$extensionsByMimeType、$extension 不匹配,因此,不允许上传,是正常的,将其扩展名修改为 png,上传成功

Figure 3


{
    "code": 10000,
    "message": "上传资源成功",
    "data": {
        "items": [
            {
                "original_file_name": "20191204113827 .png",
                "relative_path": "/tmp/2020/01/21/1579571397.5836.1739899586.png",
                "url": "http://127.0.0.1/pcs-api/storage/tmp/2020/01/21/1579571397.5836.1739899586.png"
            }
        ]
    }
}


]]>
https://www.shuijingwanwq.com/en/2020/01/21/14986/feed/ 0