【问题标题】:Timber causing fatal error in ImageHelper.php木材在 ImageHelper.php 中导致致命错误
【发布时间】: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\'];
}

screenshot of the error

我正在使用 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,它是在之前分配的...

标签: php wordpress timber


【解决方案1】:

像这样更改您的条件(将 ! 运算符添加到条件中)

if (!is_wp_error($tmp) ) {
    @unlink($file_array['tmp_name']); //line 382
    $file_array['tmp_name'] = '';
}

【讨论】:

  • 谢谢,这消除了第 382 行的错误,但我在第 387 行得到了一个新错误:致命错误:未捕获的错误:file_get_contents():参数 #1 ($filename) 必须是字符串类型,WP_Error 在 / wp-content/mu-plugins/timber/lib/ImageHelper.php 在第 387 行 $file = wp_upload_bits($locinfo['basename'], null, file_get_contents($file_array['tmp_name']));
  • if ( is_wp_error($tmp) ) { @unlink($file_array['tmp_name']); //第 382 行 $file_array['tmp_name'] = ''; }else{ // 做验证和存储的东西 $locinfo = pathinfo($loc); $file = wp_upload_bits($locinfo['basename'], null, file_get_contents($file_array['tmp_name']));返回 $file['url']; }
  • 感谢@TLEMCANI Abdelkadir 为这个而战!一系列的事情已经解决了这个错误:if ( is_wp_error($tmp) ) { //commenting out line below due to fatal error //@unlink($file_array['tmp_name']); $file_array['tmp_name'] = ''; } else { // 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']; }
【解决方案2】:

像这样更改您的条件(将 ! 运算符添加到条件中),如果条件不正确,请转到下一个代码

  if ( !is_wp_error($tmp) ) { 
     @unlink($file_array['tmp_name']); //line 382 

      $file_array['tmp_name'] = ''; 
  }else{
     // 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']; 
  }

【讨论】:

  • 您的答案可以通过额外的支持信息得到改进。请edit 添加更多详细信息,例如引用或文档,以便其他人可以确认您的答案是正确的。你可以找到更多关于如何写好答案的信息in the help center
【解决方案3】:
if ( is_wp_error($tmp) ) {
            //commenting out line below due to fatal error 
            //@unlink($file_array['tmp_name']);
            $file_array['tmp_name'] = ''; 
        } else { 
            // 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']; 
        } 

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-03-13
    • 2012-01-24
    • 1970-01-01
    相关资源
    最近更新 更多