import bulk images to wordpress

I am migrating a very big website to wordpress.
I have about 35,000 images that I need to migrate to the wordpress environment from a custom cms made in native php.

I have uploaded the files via ftp to the wp-content directory but the wordpress media does not seem to recognize it, since it’s missing the meta data in the mysql database.

Read More

I found the “add from server” plugin, but that plugin is limited to manually selecting images but when I select many images at once, it crashes.

Are there any other solution out there?

Related posts

Leave a Reply

2 comments

  1. Use RecursiveDirectoryIterator in conjunction with RecursiveIteratorIterator. Then use wp_insert_attachment to add them to the WordPress database.

    Note: I was not able to test this, but this is the general concept.

    // Get WP uploads dir
    $wp_upload_dir = wp_upload_dir();
    
    // Init recursive Obj
    $di = new RecursiveDirectoryIterator($wp_upload_dir['path']);
    foreach (new RecursiveIteratorIterator($di) as $filename => $file) {
        // get filetype for attachment meta
        $filetype = wp_check_filetype( $file->getPathname(), null );
        // attachment meta
        $attachment = array(
            'guid'           => $wp_upload_dir['url'] . '/' . $filename, 
            'post_mime_type' => $filetype['type'],
            'post_title'     => preg_replace( '/.[^.]+$/', '', $filename ),
            'post_content'   => '',
            'post_status'    => 'inherit'
        );
    
        // Insert the attachment.
        $attach_id = wp_insert_attachment( $attachment, $filename );
    }
    
  2. When I was migrating a site with more than 55345 articles with images, I wrote some pure PHP scripts that perform migration, and then inserted images as follows (based on the explanation given by jackreichert):

    $uploads = wp_upload_dir();
    $save_path = $uploads['basedir'].'/importecms/'.$new_filename;
    
    $attach_id = wp_insert_attachment( $artdata, $save_path, $post_id );
    
    if ($attach_data = wp_generate_attachment_metadata( $attach_id, $save_path)) {
        wp_update_attachment_metadata($attach_id, $attach_data);
    }
    
    $rows_affected = $wpdb->insert($wpdb->prefix.'postmeta', array('post_id' => $post_id, 'meta_key' => '_thumbnail_id', 'meta_value' => $attach_id));