title: how to upload an image on ACF with update_field on wordpress
body: <p>I have a form, which has an upload (<code>$_FILES['watch_photo']</code>) field.</p>
  
  <p>I have looked around and came to put this function together.</p>
  
  <p>It basically takes all relevant information so it is re-usable in the future, it will return an array of the $pid, and URL of the file, when it is done.</p>
  
  <p>The problem is that ACF has not provided much information how to add images to it's fields using <code>update_field()</code> <a href="http://www.advancedcustomfields.com/resources/functions/update_field/">http://www.advancedcustomfields.com/resources/functions/update_field/</a> </p>
  
  <pre><code>function my_update_attachment($f,$pid,$t='',$c='') {
    wp_update_attachment_metadata( $pid, $f );
    if( !empty( $_FILES[$f]['name'] )) { //New upload
      require_once( ABSPATH . 'wp-admin/includes/file.php' );
  
      $override['action'] = 'editpost';
      $file = wp_handle_upload( $_FILES[$f], $override );
  
      if ( isset( $file['error'] )) {
        return new WP_Error( 'upload_error', $file['error'] );
      }
  
      $file_type = wp_check_filetype($_FILES[$f]['name'], array(
        'jpg|jpeg' =&gt; 'image/jpeg',
        'gif' =&gt; 'image/gif',
        'png' =&gt; 'image/png',
      ));
      if ($file_type['type']) {
        $name_parts = pathinfo( $file['file'] );
        $name = $file['filename'];
        $type = $file['type'];
        $title = $t ? $t : $name;
        $content = $c;
  
        $attachment = array(
          'post_title' =&gt; $title,
          'post_type' =&gt; 'attachment',
          'post_content' =&gt; $content,
          'post_parent' =&gt; $pid,
          'post_mime_type' =&gt; $type,
          'guid' =&gt; $file['url'],
        );
  
        foreach( get_intermediate_image_sizes() as $s ) {
          $sizes[$s] = array( 'width' =&gt; '', 'height' =&gt; '', 'crop' =&gt; true );
          $sizes[$s]['width'] = get_option( "{$s}_size_w" ); // For default sizes set in options
          $sizes[$s]['height'] = get_option( "{$s}_size_h" ); // For default sizes set in options
          $sizes[$s]['crop'] = get_option( "{$s}_crop" ); // For default sizes set in options
        }
  
        $sizes = apply_filters( 'intermediate_image_sizes_advanced', $sizes );
  
        foreach( $sizes as $size =&gt; $size_data ) {
          $resized = image_make_intermediate_size( $file['file'], $size_data['width'], $size_data['height'], $size_data['crop'] );
          if ( $resized )
            $metadata['sizes'][$size] = $resized;
        }
  
        $attach_id = wp_insert_attachment( $attachment, $file['file'] /*, $pid - for post_thumbnails*/);
  
        if ( !is_wp_error( $id )) {
          $attach_meta = wp_generate_attachment_metadata( $attach_id, $file['file'] );
          wp_update_attachment_metadata( $attach_id, $attach_meta );
        }
  
        return array(
          'pid' =&gt;$pid,
          'url' =&gt;$file['url']
        );
        // update_post_meta( $pid, 'a_image', $file['url'] );
      }
    }
  }
  </code></pre>
  
  <p>and then I used the following code: </p>
  
  <ul>
  <li>to create a post and then</li>
  <li>upload then use <code>my_update_attachment</code> to process the image </li>
  <li>and finally update the advanced custom field </li>
  </ul>
  
  <p>The code:</p>
  
  <pre><code>$args= array(   'post_type' =&gt; 'watches',       'post_status' =&gt; 'publish'    );
  
  $watch = wp_insert_post($args);
  $att = my_update_attachment('watch_image',$watch);
  update_field('field_512e085a9372a',$att['url'],$watch);
  </code></pre>
  
  <p>What am I missing? any help would be greatly appreciated.</p>