没有不值得去解决的问题,也没有不值得去学习的技术!

Resolving .jpg Upload Failures in Yii 2.0 When the Extension Is Already Allowed

Uploading a file with the extension .jpg failed with the message: not allowed

1. List of acceptable file extensions for upload: 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. Uploading a file with the extension .jpg failed with the message: not allowed, as shown in Figure 1

Uploading a file with the extension .jpg failed with the message: not allowed
Figure 1
JSON

{
    "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。"
}

 

3. Assign the array of UploadedFile instances to Upload::files and print it out

Plaintext

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, and modify the method: validateExtension($file) to enable debugging. Request again, and the output is: 2

PHP

    /**
     * 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 determine the file extension by the file’s MIME type. If the file extension determined by the MIME type does not match the given file’s extension, the file will be considered invalid. It defaults to true, meaning this check is performed. Print the values of $mimeType, $extensionsByMimeType, and $extension in sequence; their values are respectively: as shown in Figure 2

checkExtensionByMimeType: Whether to determine the file extension by the file's MIME type. If the file extension determined by the MIME type does not match the given file's extension, the file will be considered invalid. It defaults to true, meaning this check is performed. Print the values of $mimeType, $extensionsByMimeType, and $extension in sequence; their values are respectively:
Figure 2
Plaintext

image/png

Array
(
    [0] => png
)

jpg

6. Analyzing the results, because $extensionsByMimeType and $extension do not match, the upload is not allowed. This is expected behavior. After changing the file extension to png, the upload succeeds, as shown in Figure 3

Analyzing the results, because $extensionsByMimeType and $extension do not match, the upload is not allowed. This is expected behavior. After changing the file extension to png, the upload succeeds
Figure 3
JSON

{
    "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"
            }
        ]
    }
}

关于作者 · 持续学习与技术实践

我是一名拥有 15 年以上开发经验的技术从业者,自 2013 年起持续运营个人技术博客,记录工作与个人项目中遇到的真实问题,以及解决问题过程中的探索、实践和思考。

除了技术写作,我也在持续开发和维护自己的独立项目,探索新技术和新工具在实际场景中的应用。我始终相信,没有不值得去解决的问题,也没有不值得去学习的技术。

如果这篇文章对你有所帮助,欢迎浏览博客中的其他文章,也欢迎交流技术经验与想法。

关于我  ·  GitHub  ·  邮件联系