php接受android封装对象的 多文件上传的multipart/related 相关


前端用android封装的类上传多张图片或其它文件。开始出错。


1、我这边用postman的各种方法都可以。

2、服务端是这样的:

public function file_upload_multi(){

    $files = request()->file('file');  //传上来是参数 file[] ,一个数组
 //   dump($files);
 //   die(remsg('0000', '文件名不存在!',$files)) ;
    if ($_FILES["file"]['name']==null ){
       die(remsg('0001', '文件名不存在!',"")) ;
     }
  //  dump ($files);
    $item  = [];
    foreach( $files as $file ) {
 //       dump($file);
   //     die(remsg('0000', '',$this->getFileInfo($file)  )) ;
      $info = $file->move(ROOT_PATH . 'public' . DS . '/uploads/yy'); // 存放目录
       if ($info)
        {
              $item[]='/uploads/yy/' . date('Ymd') . '/' . $info->getFilename();
          }

  }
    $data['yy_url'] =$item;
    $data['http'] = config("imgurl");
 //   $data="";
    empty($data) && die(remsg('0001', '查询失败!',$data));//结果为空
    remsg('0000', '查询成功',$data);
}

3、安卓端是这样的




安卓端打出的收到的文件dump:服务端执行第一行代码之后dump($files)

[0] => object(think\File)#6 (13) {
        ["error":"think\File":private] => string(0) ""
        ["filename":protected] => string(14) "/tmp/phpLkobl2"
        ["saveName":protected] => NULL
        ["rule":protected] => string(4) "date"
        ["validate":protected] => array(0) {
        }
        ["isTest":protected] => NULL
        ["info":protected] => array(6) {
          ["key"] => string(4) "file"
          ["name"] => string(23) "2018-05-25-09-55-13.jpg"
          ["type"] => string(19) "multipart/form-data"
          ["tmp_name"] => string(14) "/tmp/phpLkobl2"
          ["error"] => int(0)
          ["size"] => int(1462520)
        }
        ["hash":protected] => array(0) {
        }
        ["pathName":"SplFileInfo":private] => string(14) "/tmp/phpLkobl2"
        ["fileName":"SplFileInfo":private] => string(9) "phpLkobl2"
        ["openMode":"SplFileObject":private] => string(1) "r"
        ["delimiter":"SplFileObject":private] => string(1) ","
        ["enclosure":"SplFileObject":private] => string(1) """
      }
      [1] => object(think\File)#9 (13) {
        ["error":"think\File":private] => string(0) ""
        ["filename":protected] => string(14) "/tmp/phpghANer"
        ["saveName":protected] => NULL
        ["rule":protected] => string(4) "date"
        ["validate":protected] => array(0) {
        }
        ["isTest":protected] => NULL
        ["info":protected] => array(6) {
          ["key"] => string(4) "file"
          ["name"] => string(36) "PictureSelector_20180504_134103.JPEG"
          ["type"] => string(19) "multipart/form-data"
          ["tmp_name"] => string(14) "/tmp/phpghANer"
          ["error"] => int(0)
          ["size"] => int(2021206)
        }
        ["hash":protected] => array(0) {
        }
        ["pathName":"SplFileInfo":private] => string(14) "/tmp/phpghANer"
        ["fileName":"SplFileInfo":private] => string(9) "phpghANer"
        ["openMode":"SplFileObje

postman访问接口后的返回
 
 
object(think\File)[6]
  private 'error' =>  '' (length=0)
  protected 'filename' =>  'G:\xampp\tmp\phpC9D1.tmp' (length=24)
  protected 'saveName' => null
  protected 'rule' =>  'date' (length=4)
  protected 'validate' => 
    array (size=0)
      empty
  protected 'isTest' => null
  protected 'info' => 
    array (size=6)
      'key' =>  'file' (length=4)
      'name' =>  '精子形态学评价及形态学分类研究.pdf - 期刊论文 - 道客巴巴.mhtml' (length=85)
      'type' =>  'multipart/related' (length=17)
      'tmp_name' =>  'G:\xampp\tmp\phpC9D1.tmp' (length=24)
      'error' =>  0
      'size' =>  948653
  protected 'hash' => 
    array (size=0)
      empty
  private 'pathName'  =>  'G:\xampp\tmp\phpC9D1.tmp' (length=24)
  private 'fileName'  =>  'phpC9D1.tmp' (length=11)
  private 'openMode'  =>  'r' (length=1)
  private 'delimiter'  =>  ',' (length=1)
  private 'enclosure'  =>  '"' (length=1)
 
 
object(think\File)[9]
  private 'error' =>  '' (length=0)
  protected 'filename' =>  'G:\xampp\tmp\phpC9E1.tmp' (length=24)
  protected 'saveName' => null
  protected 'rule' =>  'date' (length=4)
  protected 'validate' => 
    array (size=0)
      empty
  protected 'isTest' => null
  protected 'info' => 
    array (size=6)
      'key' =>  'file' (length=4)
      'name' =>  '新建文本文档.txt' (length=22)
      'type' =>  'text/plain' (length=10)
      'tmp_name' =>  'G:\xampp\tmp\phpC9E1.tmp' (length=24)

以上的对比是:multipart/xxx的差异。
**********android端,将 multipart/form-data改为multipart/related就可以上传了。**************



以下是背景知识点:

http协议对mime类型有详细描述,multipart/....是单个消息头包含多个消息体的解决方案。multipart媒体类型对发送非文本的各媒体类型是有用的。目前常用的有这些subtype:

Messages with multiple parts  multipart/mixed
Messages with multiple, alternative parts  multipart/alternative
Message with multiple, related parts  multipart/related
Multiple parts are digests  multipart/digest
For reporting of email status (admin.)  multipart/report
Order of parts does not matter  multipart/parallel
Macintosh file data  multipart/appledouble
Aggregate messages; descriptor as header  multipart/header-set
Container for voice-mail  multipart/voice-message
HTML FORM data (see Ch. 9 and App. B)  multipart/form-data
Infinite multiparts - See Chapter 9 (Netscape)  multipart/x-mixed-replace

任何一种执行时无法识别的multipart子类型都被视为子类型"mixed"。multipart媒体类型有共同的格式,一般是这样:

Content-Type: multipart/mixed; boundary=xxxxx

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值