【发布时间】:2022-09-28 09:13:45
【问题描述】:
木材支撑问题。 博客作者页面 (/blog/authors/author-name/) 抛出此错误:
致命错误:未捕获错误:unlink():参数 #1 ($filename) 必须是字符串类型,给出 WP_Error
错误源自 ImageHelper (timber/lib/ImageHelper.php) 第 382 行
/**
* downloads an external image to the server and stores it on the server
*
* @param string $file the URL to the original file
* @return string the URL to the downloaded file
*/
public static function sideload_image( $file ) {
$loc = self::get_sideloaded_file_loc($file);
if ( file_exists($loc) ) {
return URLHelper::file_system_to_url($loc);
}
// Download file to temp location
if ( !function_exists(\'download_url\') ) {
require_once ABSPATH.\'/wp-admin/includes/file.php\';
}
$tmp = download_url($file);
preg_match(\'/[^\\?]+\\.(jpe?g|jpe|gif|png)\\b/i\', $file, $matches);
$file_array = array();
$file_array[\'name\'] = basename($matches[0]);
$file_array[\'tmp_name\'] = $tmp;
// If error storing temporarily, unlink
if ( is_wp_error($tmp) ) {
@unlink($file_array[\'tmp_name\']); //line 382
$file_array[\'tmp_name\'] = \'\';
}
// do the validation and storage stuff
$locinfo = pathinfo($loc);
$file = wp_upload_bits($locinfo[\'basename\'], null,
file_get_contents($file_array[\'tmp_name\']));
return $file[\'url\'];
}
我正在使用 WordPress 6.0.2、PHP 8.0、Timber 3.4.2。我已经使用 Composer 升级到最新版本。
该错误最近才出现,我怀疑是最新的 WordPress 更新。有没有人对潜在的修复有任何想法?谢谢!
-
这一定意味着
$tmp = download_url($file);返回了一个 WP_Error 实例,而不是实际的文件名/路径。这段代码的作用似乎没有多大意义 - 它明确检查$tmp是否是 WP_Error 的实例,然后尝试取消链接$file_array[\'tmp_name\']- 它是$tmp,它是在之前分配的...