title: wordpress custom post attachment fields
body: <p>I am creating custom post fields all text fields are working fine but i am little bit confused in saving attachment field. its saving file name in database but not moving that file in upload directory .<img src="https://i.stack.imgur.com/w7Cz6.png" alt="enter image description here"></p>
  
  <p>here is the code :</p>
  
  <p>`   $sp_boxes = array (
      'Product Details' => array (</p>
  
  <pre><code>    array( 'author', 'Author / product:' ),  //text field
      array( 'filesize', 'File size / license:' ),//text field
       array( 'abc', 'Requirements: ' ),//text field
       array( 'screen', 'Screen Shots: ',"img" ),//Attachment Field
  
  ),`
  </code></pre>
  
  <p>* Attachemnet (upload image field is confusing )
     <code>add_action( 'admin_menu', 'sp_add_custom_box' );</code>
      // Use the save_post action to do something with the data entered
      // Save the custom fields
      <code>add_action( 'save_post', 'sp_save_postdata', 1, 2 );</code>
      // Adds a custom section to the "advanced" Post and Page edit screen`</p>
  
  <p>`    function sp_add_custom_box() {
      global $sp_boxes;</p>
  
  <pre><code>if ( function_exists( 'add_meta_box' ) ) {
  
      foreach ( array_keys( $sp_boxes ) as $box_name ) {
          add_meta_box( $box_name, __( $box_name, 'sp' ), 'sp_post_custom_box', 'post',  'normal', 'high' );
      }
  }
  }
  
  function sp_post_custom_box ( $obj, $box ) {
  global $sp_boxes;
  static $sp_nonce_flag = false;
  
  // Run once
  if ( ! $sp_nonce_flag ) {
      echo_sp_nonce();
      $sp_nonce_flag = true;
  }
  
  // Genrate box contents
  foreach ( $sp_boxes[$box['id']] as $sp_box ) {
      echo field_html( $sp_box );
  }
  }
  
  function field_html ( $args ) {
  
   switch ( $args[2] ) {
  
      case 'textarea':
          return text_area( $args );
  
      case 'checkbox':
          // To Do
  
      case 'radio':
          // To Do
  
      case 'text':
      case 'img':
      return attachment( $args );
      default:
          return text_field( $args );
  }
  }
  
  function attachment ( $args ) {
  global $post;
  
  // adjust data
  $args[2] = get_post_meta($post-&gt;ID, $args[0], true);
  $args[1] = __($args[1], 'sp' );
  
  $label_format =
        '&lt;label for="%1$s"&gt;%2$s&lt;/label&gt;&lt;br /&gt;'
      . '&lt;input type="file" id="%1$s" name="%1$s" value="" size="25"&gt;&lt;br /&gt;&lt;br /&gt;';
  
  return vsprintf( $label_format, $args );
  }
  function text_field ( $args ) {
  global $post;
  
  // adjust data
  $args[2] = get_post_meta($post-&gt;ID, $args[0], true);
  $args[1] = __($args[1], 'sp' );
  
  $label_format =
        '&lt;label for="%1$s"&gt;%2$s&lt;/label&gt;&lt;br /&gt;'
      . '&lt;input style="width: 95%%;" type="text" name="%1$s" value="%3$s" /&gt;&lt;br /&gt;&lt;br    /&gt;';
  
  return vsprintf( $label_format, $args );
  </code></pre>
  
  <p>}</p>
  
  <p>`</p>
  
  <ul>
  <li>And this is how i am saving post data</li>
  </ul>
  
  <p>`    function sp_save_postdata($post_id, $post) {
      global $sp_boxes;</p>
  
  <pre><code>// verify this came from the our screen and with proper authorization,
  // because save_post can be triggered at other times
  if ( ! wp_verify_nonce( $_POST['sp_nonce_name'], plugin_basename(__FILE__) ) ) {
      return $post-&gt;ID;
  }
  
  // Is the user allowed to edit the post or page?
  if ( 'page' == $_POST['post_type'] ) {
      if ( ! current_user_can( 'edit_page', $post-&gt;ID ))
          return $post-&gt;ID;
  
  } else {
      if ( ! current_user_can( 'edit_post', $post-&gt;ID ))
          return $post-&gt;ID;
  }
  
  // OK, we're authenticated: we need to find and save the data
  // We'll put it into an array to make it easier to loop though.
  
  // The data is already in $sp_boxes, but we need to flatten it out.
  foreach ( $sp_boxes as $sp_box ) {
      foreach ( $sp_box as $sp_fields ) {
          $my_data[$sp_fields[0]] =  $_POST[$sp_fields[0]];
      }
  }
  
  // Add values of $my_data as custom fields
  // Let's cycle through the $my_data array!
  foreach ($my_data as $key =&gt; $value) {
      if ( 'revision' == $post-&gt;post_type  ) {
          // don't store custom data twice
          return;
      }
  
      // if $value is an array, make it a CSV (unlikely)
      $value = implode(',', (array)$value);
  
      if ( get_post_meta($post-&gt;ID, $key, FALSE) ) {
  
  
          // Custom field has a value.
          update_post_meta($post-&gt;ID, $key, $value);
  
  
      } else {
  
          // Custom field does not have a value.
          add_post_meta($post-&gt;ID, $key, $value);
      }
  
      if (!$value) {
  enter code here
          // delete blanks
          delete_post_meta($post-&gt;ID, $key);
      }
  }
  }`
  </code></pre>
  
  <p><em>All are working fine upload image field save the image name in db but image is  not moving  to upload directory.</em>
  Anyone can help me???
  thanks</p>