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


