Vaibhav Bhanushali
Vaibhav Bhanushali

Reputation: 653

PHP multiple $_FILES for forms

I am a newbie in php and using WordPress as my CMS. I am trying to enable an audio upload with desired featured image in WordPress front end form The PHP code of form is

if ($_FILES['audio']) {
  foreach ($_FILES as $file => $array) {
     $newupload = insert_attachment($file,$pid);
         $theconents = get_attachment_link($newupload);  
         $my_post = array(
                  'ID'=> $pid,
                  'post_content' => $theconents
         );
         wp_update_post( $my_post );
  }
}
 if ($_FILES['thumbnail']) {
    foreach ($_FILES as $file => $array) {
   $newuploads = insert_image($file,$pid);
   // $newuploads returns the attachment id of the file that
   // was just uploaded. Do whatever you want with that now.
   }
}

The insert_image and Insert_attachment functions are

function insert_attachment($file_handler,$post_id,$setthumb='false') {
   // check to make sure its a successful upload
   if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) __return_false();

   require_once(ABSPATH . "wp-admin" . '/includes/image.php');
   require_once(ABSPATH . "wp-admin" . '/includes/file.php');
   require_once(ABSPATH . "wp-admin" . '/includes/media.php');

   $attach_id = media_handle_upload( $file_handler, $post_id );

   return $attach_id;
}

And

function insert_image($file_handler,$post_id,$setthumb='false') {
   // check to make sure its a successful upload
   if ($_FILES[$file_handler]['error'] !== UPLOAD_ERR_OK) __return_false();

   require_once(ABSPATH . "wp-admin" . '/includes/image.php');
   require_once(ABSPATH . "wp-admin" . '/includes/file.php');
   require_once(ABSPATH . "wp-admin" . '/includes/media.php');

   $attach_id = media_handle_upload( $file_handler, $post_id );

   if ($setthumb) update_post_meta($post_id,'_thumbnail_id',$attach_id);
   return $attach_id;
  }

And the basic html is

Audio

   <input type="file"  name="audio" id="upload-audio"/>;

Audio featured Image

   <input type="file"  name="thumbnail" id="upload-audio"/>;

When i submit the form

I see only $_FILES['audio'] Works and i cant see the featured image

Upvotes: 0

Views: 258

Answers (1)

Vishnu Sharma
Vishnu Sharma

Reputation: 1383

Please write your form like this :

 <form enctype= "multipart/form-data" method="POST">
 <input type="file"  name="audio" id="upload-audio"/>;
   Audio featured Image
  <input type="file"  name="thumbnail" id="upload-audio"/>;
   <input type="submit" value="submit"/>
 </form> 

echo "<pre>";
print_r($_FILES);die;

OUTPUT:

Array
(
    [audio] => Array
        (
            [name] => contact.php
            [type] => application/x-php
            [tmp_name] => /tmp/phphjFg37
            [error] => 0
            [size] => 17983
        )

    [thumbnail] => Array
        (
            [name] => example-form.php
            [type] => application/x-php
            [tmp_name] => /tmp/phpKbxoBx
            [error] => 0
            [size] => 2112
        )

)

Upvotes: 1

Related Questions