douyaosi3164 2013-09-27 06:43
浏览 36

PHP多个$ _FILES表单

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

  • 写回答

1条回答 默认 最新

  • doupixian1436 2013-09-27 07:09
    关注

    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
            )
    
    )
    
    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图