无法上载大小超过1MB的文件

时间:2021-02-08 作者:morpheus

我们有一个多站点安装,配置如下:

bash-5.0# cat /usr/local/etc/php/conf.d/uploads.ini
file_uploads = On
memory_limit = 500M
upload_max_filesize = 100M
post_max_size = 100M
max_execution_time = 600
max_file_uploads = 50000
max_execution_time = 5000
max_input_time = 5000
我们的/usr/local/etc/php/php.ini 具有:

;;;;;;;;;;;;;;;;
; File Uploads ;
;;;;;;;;;;;;;;;;

; Whether to allow HTTP file uploads.
; http://php.net/file-uploads
file_uploads = On

; Temporary directory for HTTP uploaded files (will use system default if not
; specified).
; http://php.net/upload-tmp-dir
;upload_tmp_dir =

; Maximum allowed size for uploaded files.
; http://php.net/upload-max-filesize
upload_max_filesize = 2M

; Maximum number of files that can be uploaded via a single request
max_file_uploads = 20
但是在wp-admin 面板我们看到:

enter image description here

我们如何解决这个问题?现有的答案在这里都没有帮助。我们检查过了functions.php 文件它没有任何ini_set 打电话进来。

跑步phpinfo 我们观察到:

Loaded Configuration File => /usr/local/etc/php/php.ini
Additional .ini files parsed => /usr/local/etc/php/conf.d/docker-php-ext-bcmath.ini,
...
/usr/local/etc/php/conf.d/uploads.ini

post_max_size => 100M => 100M
upload_max_filesize => 100M => 100M

https://developer.wordpress.org/reference/functions/wp_max_upload_size/

在里面

function wp_max_upload_size() {
    $u_bytes = wp_convert_hr_to_bytes( ini_get( \'upload_max_filesize\' ) );
    $p_bytes = wp_convert_hr_to_bytes( ini_get( \'post_max_size\' ) );
 
    /**
     * Filters the maximum upload size allowed in php.ini.
     *
     * @since 2.5.0
     *
     * @param int $size    Max upload size limit in bytes.
     * @param int $u_bytes Maximum upload filesize in bytes.
     * @param int $p_bytes Maximum size of POST data in bytes.
     */
    return apply_filters( \'upload_size_limit\', min( $u_bytes, $p_bytes ), $u_bytes, $p_bytes );
}

1 个回复
最合适的回答,由SO网友:morpheus 整理而成

调试后,我们发现wp_max_upload_size 函数返回1MB。这个u_bytesp_bytes 与预期一致,等于我们uploads.ini 文件,但应用upload_size_limit 筛选器将大小更改为1MB,即问题出现在:

return apply_filters( \'upload_size_limit\', min( $u_bytes, $p_bytes ), $u_bytes, $p_bytes );
Aupload_size_limit_filter 定义于wp-includes/ms-functions.php 并连接到执行only for a multisite installation. (接线代码位于wp-includes/ms-default-filters.php. 文件名具有ms 前缀代表multisite):

// this function runs only for a multisite installation of wordpress
function upload_size_limit_filter( $size ) {
        $fileupload_maxk = KB_IN_BYTES * get_site_option( \'fileupload_maxk\', 1500 );
        if ( get_site_option( \'upload_space_check_disabled\' ) ) {
                return min( $size, $fileupload_maxk );
        }
 
        return min( $size, $fileupload_maxk, get_upload_space_available() );
}
追踪代码路径,我们发现fileupload_maxk 相等的1536000 这就是问题的根源。这个fileupload_maxk 值是从MySQL获取的。

继续,我们可以在MySQL数据库中看到这一点。The wp_sitemeta table only exists for a multisite installation:

mysql> select * from wp_sitemeta where meta_key = \'fileupload_maxk\';
+---------+---------+-----------------+------------+
| meta_id | site_id | meta_key        | meta_value |
+---------+---------+-----------------+------------+
|       7 |       1 | fileupload_maxk | 1500       |
+---------+---------+-----------------+------------+
1 row in set (0.00 sec)
所以解决这个问题的方法是更新这个值。可以使用wp-cli 如下所示。我们正在使用多站点安装,因此必须使用wp site 命令:

bash-5.0# wp site option update fileupload_maxk 10000 --allow-root
Success: Updated \'fileupload_maxk\' site option.
这将更新MySQL中的条目,问题已得到修复。简而言之,似乎fileupload_maxk 似乎并不广为人知。我们在Google上搜索原始查询时,没有一次找到它。Here 是另一个答案fileupload_maxk.