title: foreach Loop Running Once
body: <p>So I'm having an issue with my foreach loop only running once.  </p>
  
  <p>I have the following data for the $file_url variable.</p>
  
  <pre><code>$file_url = "http://www.somedomain.com/12355/1.jpg,http://www.somedomain.com/12355/2.jpg,http://www.somedomain.com/12355/3.jpg,http://www.somedomain.com/12355/4.jpg";
  </code></pre>
  
  <p>Now my code looks like this:</p>
  
  <pre><code>function fetch_media($file_url,$vin,$cacheid) {
      require_once(ABSPATH . 'wp-load.php');
      require_once(ABSPATH . 'wp-admin/includes/image.php');
      global $wpdb;
  
      if(!$vin) {
          $vin = $cacheid;
      }
  
      $vin = $vin . '/';
  
      //directory to import to    
      $artDir = "wp-content/uploads/vehiclephotos/$vin";
  
      //if the directory doesn't exist, create it 
      if(!file_exists(ABSPATH.$artDir)) {
          mkdir(ABSPATH.$artDir);
      }
  
      $file_url = explode(",", $file_url);
      $gallery_images = array();
  
      foreach ($file_url as $url) {
  
      //rename the file... alternatively, you could explode on "/" and keep the original file name
      $filename = array_pop(explode("/", $url));
  
          if (@fclose(@fopen($url, "r"))) { //make sure the file actually exists
              copy($url, ABSPATH.$artDir.$filename);
  
              $siteurl = get_option('siteurl');
              $file_info = getimagesize(ABSPATH.$artDir.$filename);
  
              //create an array of attachment data to insert into wp_posts table
              $artdata = array();
              $artdata = array(
                  'post_author' =&gt; 1, 
                  'post_date' =&gt; current_time('mysql'),
                  'post_date_gmt' =&gt; current_time('mysql'),
                  'post_title' =&gt; $filename, 
                  'post_status' =&gt; 'inherit',
                  'comment_status' =&gt; 'closed',
                  'ping_status' =&gt; 'closed',
                  'post_name' =&gt; sanitize_title_with_dashes(str_replace("_", "-", $filename)),
                  'post_modified' =&gt; current_time('mysql'),
                  'post_modified_gmt' =&gt; current_time('mysql'),
                  'post_type' =&gt; 'attachment',
                  'guid' =&gt; $siteurl.'/'.$artDir.$filename,
                  'post_mime_type' =&gt; $file_info['mime'],
                  'post_excerpt' =&gt; '',
                  'post_content' =&gt; ''
              );
  
              $uploads = wp_upload_dir();
              $save_path = $uploads['basedir'].'/vehiclephotos/'.$vin.$filename;
  
              //insert the database record
              $attach_id = wp_insert_attachment($artdata, $save_path);
  
              //generate metadata and thumbnails
              if ($attach_data = wp_generate_attachment_metadata( $attach_id, $save_path)) {
                  wp_update_attachment_metadata($attach_id, $attach_data);
              }
  
              array_push($gallery_images,$attach_id);
              }
  
      }
  
      return serialize($gallery_images);
  }
  </code></pre>
  
  <p>So the output I'm getting is the serialized array which is: </p>
  
  <pre><code>a:1:{i:0;i:103525;}
  </code></pre>
  
  <p>Which is fine, but since there were 4 array items, it should have looped 4 times and given me the serialized data with 4 attachment_id's.  All the other code in there runs fine once, it downloads the image from the URL, it renames it and creates all thumbnail sizes of the photo fine.  </p>
  
  <p>Any idea what I'm doing wrong?</p>