l12ab
V2EX  ›  PHP

咨询一个关于 curl 上传多个文件的问题

  •  
  •   l12ab · May 27, 2015 · 3979 views
    This topic created in 4032 days ago, the information mentioned may be changed or developed.

    这样的,我的目的是模拟一个html页面的表单提交。

    对方网站提供了一个页面form表单,表单里有3个文件上传框
    <input type="file" name="upload1">
    <input type="file" name="upload2">
    <input type="file" name="upload3">

    我在页面上手工操作,只上传1个或者2个文件是没有问题的,但是我用php写了curl去提交,下面这样写post参数,只上传2个
    $data = array("upload1"=>"@/file/path1", "upload2"=>"@/file/path2");

    则对方服务器返回错误,意思就是无法读取文件内容;如果
    $data = array("upload1"=>"@/file/path1", "upload2"=>"@/file/path2", "upload3"=>"");

    也返回错误。

    求如何只传1个或者2个文件

    8 replies    2015-05-27 22:49:30 +08:00
    lilydjwg
        1
    lilydjwg  
       May 27, 2015
    1. 何不贴出可复现问题的完整代码?
    2. 抓包看看。可选工具:mitmproxy、wireshark。
    l12ab
        2
    l12ab  
    OP
       May 27, 2015
    @lilydjwg 因为是在内网的机器上测试,那台内网破机器只能IE6,于是就用httpwatch分析的,分析出来upload3没有值
    fangjinmin
        3
    fangjinmin  
       May 27, 2015
    看看对方网页的JS,有可能对方的网页就没有提交upload3
    l12ab
        4
    l12ab  
    OP
       May 27, 2015
    @fangjinmin 我不提交upload3就报错,提交个空值也报错,提交其他非文件的值也报错,只有提交一个文件才正常。
    l12ab
        5
    l12ab  
    OP
       May 27, 2015
    OK 自己解决了,需要自行拼接post过去的header和content内容,具体情况可以搜一下这个别人写的函数curl_setopt_custom_postfields
    lilydjwg
        6
    lilydjwg  
       May 27, 2015
    @l12ab 那个涉及到多个同名参数,你这个没关系的呀。
    l12ab
        7
    l12ab  
    OP
       May 27, 2015
    @lilydjwg 是没关系,但是主要用到它拼接的request body
    lilydjwg
        8
    lilydjwg  
       May 27, 2015
    @l12ab 哦。所以你绕过而不是解决了问题啦。
    About   ·   Help   ·   Advertise   ·   Blog   ·   API   ·   FAQ   ·   Solana   ·   2619 Online   Highest 6679   ·     Select Language
    创意工作者们的社区
    World is powered by solitude
    VERSION: 3.9.8.5 · 49ms · UTC 15:54 · PVG 23:54 · LAX 08:54 · JFK 11:54
    ♥ Do have faith in what you're doing.