$conf, $runtime; function_exists('chdir') AND chdir(APP_PATH); $r = 'mysql' == $conf['cache']['type'] ? website_set('runtime', $runtime) : cache_set('runtime', $runtime); } function runtime_truncate() { global $conf; 'mysql' == $conf['cache']['type'] ? website_set('runtime', '') : cache_delete('runtime'); } register_shutdown_function('runtime_save'); ?>uploads - What is wrong with my wp_insert_attachment code?|Programmer puzzle solving
最新消息:Welcome to the puzzle paradise for programmers! Here, a well-designed puzzle awaits you. From code logic puzzles to algorithmic challenges, each level is closely centered on the programmer's expertise and skills. Whether you're a novice programmer or an experienced tech guru, you'll find your own challenges on this site. In the process of solving puzzles, you can not only exercise your thinking skills, but also deepen your understanding and application of programming knowledge. Come to start this puzzle journey full of wisdom and challenges, with many programmers to compete with each other and show your programming wisdom! Translated with DeepL.com (free version)

uploads - What is wrong with my wp_insert_attachment code?

matteradmin10PV0评论

I am attempting to upload company logo images to my Media Library - but I am noticing some problems...

Namely, some of those images seem to come in at 0Kb created.

Source image URLs are all like ?format=png

I wish to store them in /home/mysite/public_html/wp-content/uploads/org_logos/{companyname}.png

... wherein companyname is the slug of the corresponding company term for taxonomy Organisation.

Here is my code...

// Remote logo URL
$image_url = $json_object->logo.'?format=png';

// Generate a file path and name
$upload_dir = wp_upload_dir();
$image_data = file_get_contents( $image_url );
$filename = $company_term->slug.'.png';
if ( wp_mkdir_p( $upload_dir['path'] ) ) {
  $file = $upload_dir['path'] . '/org_logos/' . $filename;
}
else {
  $file = $upload_dir['basedir'] . '/org_logos/' . $filename;
}

// Copy the file
file_put_contents( $file, $image_data );

// Add to Media Library
$wp_filetype = wp_check_filetype( $filename, null ); // eg. image/png
$attachment = array(
  'post_mime_type' => $wp_filetype['type'],
  'post_title' => sanitize_file_name( $filename ),
  'post_content' => '',
  'post_status' => 'inherit'
);
$attach_id = wp_insert_attachment( $attachment, $file );

require_once( ABSPATH . 'wp-admin/includes/image.php' );
$attach_data = wp_generate_attachment_metadata( $attach_id, $file );
wp_update_attachment_metadata( $attach_id, $attach_data );

This is successfully adding something to both the server and the Media Library every time. But, whilst sometimes that is an actual image, sometimes it is 0Kb and/or a blank thumbnail in the Media Library.

I had thought there was an issue around using slug where it contains dashes as a filename because the resulting image file for source ?format=png (/home/mysite/public_html/wp-content/uploads/org_logos/starcom-mediavest-group.png) is always blank.

But, actually, the problem goes for more than just filenames which take slugs bearing dashes, because another problematic example is a no-dash source ?format=png wherein file /home/mysite/public_html/wp-content/uploads/org_logos/cadreon.png is also 0Kb.

Image types are confirmed as image/png.

Is the problem my file_put_contents, my WordPress code or something else?

It's weird, because it works sometimes.

Post a comment

comment list (0)

  1. No comments so far