【问题标题】:Error in image upload图片上传错误
【发布时间】:2012-11-23 07:48:05
【问题描述】:

我想发布一些数据,包括一张图片。为此我在 php 中使用 curl。我的代码如下-

 $ch = curl_init();
    curl_setopt($ch, CURLOPT_HEADER, 0);
    curl_setopt($ch, CURLOPT_VERBOSE, 0);
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
    curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: multipart/form-data"));
    curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; Linux i686; rv:6.0) Gecko/20100101 Firefox/6.0Mozilla/4.0 (compatible;)");
    curl_setopt($ch, CURLOPT_URL, 'http://postacity.co.uk:8080/shine/pp/upload/');
    curl_setopt($ch, CURLOPT_POST, true);
    $post = array(
        'creatorid' => '119',
        'userfile' => '@/temp/fgf.jpg',
        'notice' => 'Image1',
        'catid' => '1',
        'title' => 'bookofzeus',
        'boardid' => '332',
    );
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
    $response = curl_exec($ch);
    if(curl_errno($ch)) {
        echo 'Error: ' . curl_error($ch);
    }
    else {
        echo $response;
    }
    }

每次我都遇到同样的错误

错误:创建表单数据失败

我是不是做错了什么。我已经搜索过这个问题,但仍然没有找到解决方案。

【问题讨论】:

    标签: php curl multipartform-data


    【解决方案1】:

    文件路径有问题:'@/temp/fgf.jpg'

    你应该看看这个错误描述:Bug 50060 - failed creating formpost data if post array value starts with @

    这个问题的解决方案,例如php freaks

    并使用绝对文件路径。

    【讨论】:

      【解决方案2】:

      参考它的工作

      <?php
          $ch = curl_init();
          curl_setopt($ch, CURLOPT_HEADER, 0);
          curl_setopt($ch, CURLOPT_VERBOSE, 0);
          curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
          curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/4.0 (compatible;)");
          curl_setopt($ch, CURLOPT_URL, _VIRUS_SCAN_URL);
          curl_setopt($ch, CURLOPT_POST, true);
          // same as <input type="file" name="file_box">
          $post = array(
              "file_box"=>"@/path/to/myfile.jpg",
              "username"=>"foobar",
              "password"=>"secret",
              "submit"=>"submit"
          );
          curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
          $response = curl_exec($ch);
      ?>
      

      也参考这个网址的

      http://joshhighland.com/blog/2010/11/27/using-curl-and-php-to-upload-files-through-a-form-post/

      http://blogs.digitss.com/php/curl-php/posting-or-uploading-files-using-curl-with-php/

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-03-08
        • 2016-01-02
        • 2018-06-22
        • 2013-07-07
        • 2015-11-02
        相关资源
        最近更新 更多