admin管理员组文章数量:1426491
I have enabled SVG uploads using this code:
add_filter('upload_mimes', function($mimes) {
$mimes['svg'] = 'image/svg+xml';
return $mimes;
});
However, uploads of SVG files that start with the <svg>
tag fail with the usual "Sorry, this file type is not permitted for security reasons." error that WordPress displays when SVG uploads are not supported.
If I add <?xml version="1.0" encoding="UTF-8" standalone="no"?>
to the file, just before the opening <svg>
tag, the upload succeeds.
Why is the XML tag required? Is this requirement normal in WordPress, or is there something wrong with my setup?
I have enabled SVG uploads using this code:
add_filter('upload_mimes', function($mimes) {
$mimes['svg'] = 'image/svg+xml';
return $mimes;
});
However, uploads of SVG files that start with the <svg>
tag fail with the usual "Sorry, this file type is not permitted for security reasons." error that WordPress displays when SVG uploads are not supported.
If I add <?xml version="1.0" encoding="UTF-8" standalone="no"?>
to the file, just before the opening <svg>
tag, the upload succeeds.
Why is the XML tag required? Is this requirement normal in WordPress, or is there something wrong with my setup?
Share Improve this question asked Jun 14, 2019 at 19:08 Theo d'OrTheo d'Or 1782 silver badges6 bronze badges2 Answers
Reset to default 6It seems that in the recent releases of WordPress, changes were made to the mime type handling to make sure that files have the extension they say they do: https://make.wordpress/core/2018/12/13/backwards-compatibility-breaks-in-5-0-1/
This poses an issue for SVG files without the tag in them.
SVG is actually an XML, and WordPress is now requiring to have a line such as
<?xml version="1.0" encoding="utf-8"?>
in an SVG file.
To validate uploads WordPress compares the MIME type of the file to the allowed MIME types for that extension. So when the file is uploaded, WordPress checks for the file extension, .svg
, and the file's MIME type. It then these against the allowed MIME type for the .svg
extension. If the detected MIME type does not match, then the upload is refused. The purpose of this is to prevent dangerous files being uploaded with a misleading file extension.
The actual detection of the MIME type for the file is ultimately handled by PHP, though. So if your SVG file is not being detected as image/svg+xml
, then this is because PHP doesn't recognise it as an SVG file. As you've discovered, it appears that PHP does not recognise files without the <?xml ?>
tag as an SVG. It's likely that it thinks the file is an HTML file, text/html
. This would be because HTML documents can contain <svg>
elements, meaning only way to reliably distinguish between an HTML file with SVG and an actual SVG file is the presence of this tag.
So this is why the tag needs to be included. It's what makes it an image/svg+xml
file.
本文标签: Why does SVG upload in Media Library fail if the file does not have an XML tag at the beginning
版权声明:本文标题:Why does SVG upload in Media Library fail if the file does not have an XML tag at the beginning? 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745400245a2657002.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论