PHP多个$ _FILES表单

时间:2013-09-27 06:43:28

标签: php wordpress file-upload

我是php的新手,使用WordPress作为我的CMS。 我正在尝试使用WordPress前端表单中的所需特色图像启用音频上传 表单的PHP代码是

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.
   }
}

insert_image和Insert_attachment函数是

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;
}

并且

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;
  }

基本的html是

音频

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

音频精选图片

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

当我提交表格时

我只看到$_FILES['audio']有效 我无法看到特色图片

1 个答案:

答案 0 :(得分:1)

请写下这样的表格:

 <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;

输出:

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
        )

)