You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

WordPress迁移后出现PHP非数值警告 求技术支持

WordPress迁移后PHP非数值警告问题解决

迁移测试版WordPress站点到新主机后,网站头部出现以下警告:

Warning: A non-numeric value encountered in /webroot/l/r/lrcso001/primary/www/wp-includes/media.php on line 467
Warning: A non-numeric value encountered in /webroot/l/r/lrcso001/primary/www/wp-includes/media.php on line 477

相关代码片段(wp-includes/media.php第450-487行):

if ( IMAGETYPE_GIF == $orig_type ) {
        if ( !imagegif( $newimage, $destfilename ) )
            return new WP_Error('resize_path_invalid', __( 'Resize path invalid' ));
    } elseif ( IMAGETYPE_PNG == $orig_type ) {
        if ( !imagepng( $newimage, $destfilename ) )
            return new WP_Error('resize_path_invalid', __( 'Resize path invalid' ));
    } else {
        // all other formats are converted to jpg
        $destfilename = "{$dir}/{$name}-{$suffix}.jpg";
        if ( !imagejpeg( $newimage, $destfilename, apply_filters( 'jpeg_quality', $jpeg_quality, 'image_resize' ) ) )
            return new WP_Error('resize_path_invalid', __( 'Resize path invalid' ));
    }

    imagedestroy( $newimage );

    // Set correct file permissions
    $stat = stat( dirname( $destfilename ));
    $perms = $stat['mode'] & 0000666; //same permissions as parent folder, strip off the executable bits
    @ chmod( $destfilename, $perms );

    return $destfilename;
}

/**
 * Resize an image to make a thumbnail or intermediate size.
 *
 * The returned array has the file size, the image width, and image height. The
 * filter 'image_make_intermediate_size' can be used to hook in and change the
 * values of the returned array. The only parameter is the resized file path.
 *
 * @since 2.5.0
 *
 * @param string $file File path.
 * @param int $width Image width.
 * @param int $height Image height.
 * @param bool $crop Optional, default is false. Whether to crop image to specified height and width or resize.
 * @return bool|array False, if no image was created. Metadata array on success.
 */

解决步骤

1. 临时隐藏前端警告(应急方案)

编辑站点根目录的wp-config.php文件:

  • 找到define('WP_DEBUG', true);,修改为:
    define('WP_DEBUG', false);
    define('WP_DEBUG_DISPLAY', false);
    
  • 如果没有WP_DEBUG相关配置,直接添加上述代码到文件末尾(require_once(ABSPATH . 'wp-settings.php');之前)。

2. 修复根源问题(针对media.php报错)

报错原因是stat()函数获取目录信息失败,导致$stat['mode']不是数值。修改wp-includes/media.php中的权限设置代码:
找到这段代码:

// Set correct file permissions
$stat = stat( dirname( $destfilename ));
$perms = $stat['mode'] & 0000666; //same permissions as parent folder, strip off the executable bits
@ chmod( $destfilename, $perms );

替换为:

// Set correct file permissions
$dir_path = dirname( $destfilename );
$stat = stat( $dir_path );
if ( $stat && isset( $stat['mode'] ) ) {
    $perms = $stat['mode'] & 0000666; //same permissions as parent folder, strip off the executable bits
    @ chmod( $destfilename, $perms );
}

3. 其他排查方向

  • 检查PHP版本:联系新主机商确认PHP版本,WordPress官方推荐PHP7.4~8.1版本,若版本过高或过低,请求调整。
  • 替换受损文件:从WordPress官方下载对应版本的安装包,提取wp-includes/media.php文件,通过FTP替换当前站点的对应文件。
  • 调整文件权限:确保wp-content/uploads目录权限为755,目录内文件权限为644,可通过主机控制面板或FTP工具修改。

内容的提问来源于stack exchange,提问作者Michael Kovach

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.06.15 23:05:00