Title: Attach image from a folder while inserting post from CSV file
Last modified: August 24, 2016

---

# Attach image from a folder while inserting post from CSV file

 *  [noorahmad](https://wordpress.org/support/users/noorahmad/)
 * (@noorahmad)
 * [11 years ago](https://wordpress.org/support/topic/attach-image-from-a-folder-while-inserting-post-from-csv-file/)
 * Hello,
 * I have a CVS file with almost 5000 records and I want to insert it into custom
   post with custom fields (meta). I have a column (attachment) image type and I
   want to insert and attach image into the post.
 * I have all images in uploads/2015/05 folder.
 * So far my code:
 * `$file = fopen( get_template_directory() . ‘/data.csv’, ‘r’);
    while (($line 
   = fgetcsv($file)) !== FALSE) { $title = $line[0]; $content = $line[2]; $time 
   = get_the_time(‘l, F jS, Y’); $my_post = array( ‘post_title’ => $title, ‘post_date’
   => $time, ‘post_content’ => $content, ‘post_status’ => ‘publish’, ‘post_type’
   => ‘abc’, ); $the_post_id = wp_insert_post( $my_post ); __update_post_meta( $
   the_post_id, ‘abc’, $abc); $file = $line[3]; // abc.png // Here I want to attach
   the attachment the image abc.png where its location in uploads/2015/05 folder.
 * Please guide.
 * Thanks,
    Noor

The topic ‘Attach image from a folder while inserting post from CSV file’ is closed
to new replies.

## Tags

 * [attachment](https://wordpress.org/support/topic-tag/attachment/)
 * [custom post](https://wordpress.org/support/topic-tag/custom-post/)

 * In: [Fixing WordPress](https://wordpress.org/support/forum/how-to-and-troubleshooting/)
 * 0 replies
 * 1 participant
 * Last reply from: [noorahmad](https://wordpress.org/support/users/noorahmad/)
 * Last activity: [11 years ago](https://wordpress.org/support/topic/attach-image-from-a-folder-while-inserting-post-from-csv-file/)
 * Status: not resolved

## Topics

### Topics with no replies

### Non-support topics

### Resolved topics

### Unresolved topics

### All topics
