Programmatically adding images to the media library with wp_generate_attachment_metadata randomly fails

I am running the latest WordPress version (4.6) on a LAMP server (shared hosting) with PHP version 5.6.12.

I am trying to programmatically add 10 images, uploaded with ftp to the wp-uploads directory, to the media library using the three WordPress functions wp_insert_attachment, wp_generate_attachment_metadata and wp_update_attachment_metadata.

My problem:

Sometimes, my PHP script works (all 10 images are added to the media library correctly) – and sometimes it does not (out of the 10 images, only 4, 5, 6 or so are added)! Each images is 2M – 4M in size.

What I’ve done so far:

I enabled error_logging via php.ini and found out that every once in a while wp_generate_attachment_metadata fails (i.e. while handling the 5th, 6th, 7th or so image) and my entire PHP script terminates. I do not get any more information from the error_log() other than that. Since I suspected memory issues, I increased the memory size for php to 120M (my hosting providers gives me 128M) and script execution to 100s (my hosting provider gives me 120s).
All files exist (of course), they are all PNGs – and, as I said, using the same set of 10 images for testing, it sometimes works and sometimes it doesn’t work…

My question:

  • Is there a known problem with wp_generate_attachment_metadata in WP 4.6? Everything used to work fine until I upgraded my site from WP 4.3 to 4.6…

  • If not enough memory is causing the problem, how could I optimize my PHP script to handle the 128M memory limit given by my web hoster?

  • How do I find out if lack of memory causes my PHP script to terminate?

Thanks in advance!

Here is my code:

$post_id = 1234;
$images = array('filename1.png', 'filename2.png', ... 'filename10.png');

for($i = 0; $i < 10; $i++) {
  $attachment = array(
    'post_mime_type' => 'image/png',
    'post_title' => 'my description',
    'post_content' => 'my description',
    'post_status' => 'inherit'
  );
  $image_id = wp_insert_attachment($attachment, $images[$i], $post_id);
  $image_data = wp_generate_attachment_metadata($image_id, $images[$i]);
  wp_update_attachment_metadata($image_id, $image_data);
}

Solutions Collecting From Web of "Programmatically adding images to the media library with wp_generate_attachment_metadata randomly fails"

I have checked your code, and I think you are missing the guid of the images. Please have a look at the code below:


$post_id = 1234;
$images = array('filename1.png', 'filename2.png', ... 'filename10.png');

// Get the path to the upload directory.
$wp_upload_dir = wp_upload_dir();

foreach($images as $name) {
    $attachment = array(
        'guid'=> $wp_upload_dir['url'] . '/' . basename( $name ), 
        'post_mime_type' => 'image/png',
        'post_title' => 'my description',
        'post_content' => 'my description',
        'post_status' => 'inherit'
         );
$image_id = wp_insert_attachment($attachment, $name, $post_id);
// Make sure that this file is included, as wp_generate_attachment_metadata() depends on it. require_once( ABSPATH . 'wp-admin/includes/image.php' );
// Generate the metadata for the attachment, and update the database record. $attach_data = wp_generate_attachment_metadata( $image_id, $name );
wp_update_attachment_metadata( $image_id, $attach_data );
}

For detail look the wp_insert_attachment function.