skip to Main Content

Timber support question.
Blog author pages (/blog/authors/author-name/) are throwing this error:

Fatal error: Uncaught Error: unlink(): Argument #1 ($filename) must be of type string, WP_Error given

The error originates from ImageHelper (timber/lib/ImageHelper.php) line 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

I’m using WordPress 6.0.2, PHP 8.0, Timber 3.4.2. I’ve upgraded to newest version using Composer.

The error has appeared only recently, I suspect with the latest WordPress update. Has anyone got any ideas for a potential fix? Thank you!

3

Answers


  1. Chosen as BEST ANSWER
    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. change your condition like this (add ! operator to the condition)

    if (!is_wp_error($tmp) ) {
        @unlink($file_array['tmp_name']); //line 382
        $file_array['tmp_name'] = '';
    }
    
    Login or Signup to reply.
  3. change your condition like this (add ! operator to the condition), and if the condition incorrect go to the next code

      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']; 
      }
    
    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search