开发者

posting file "multipart/form-data" through php

Im trying to use imageshshack api in php

in the following way:

<?php
function do_post_request($url, $data, $optional_headers = null) {
  $params = array('http' => array(
              'method' => 'POST',
              'content' => $data
            ));
  if ($optional_headers !== null) {
    $params['http']['header'] = $optional_headers;
  }
  $ctx = stream_context_create($params);
  $fp = @fopen($url, 'rb', false, $ctx);
  if (!$fp) {
    throw new Exception("Problem with $url, $php_errormsg");
  }
  $response = @stream_get_contents($fp);
  if ($response === false) {
    throw new Exception("Problem reading data from $url, $php_errormsg");
  }
  return $response;
}

    $fileName = $_FILES['picture']['name'];
    $tmpName = $_FILES['picture']['tmp_name'];
    $fileSize = $_FILES['picture']['size'];
    $fileType = $_FILES['picture']['type'];
    $fo = @fopen($tmpName, "r");
    $imgposted = @fread($fo, filesize($tmpName));
    $imgposted = addslashes($imgposted);

    $data = 'fileupload='.$imgposted.'&rembar=开发者_C百科1&key=******';
    echo do_post_request('http://www.imageshack.us/upload_api.php',$data);

?>

but the data is being posted normally, I wanted to be posted like if I had a form with this attribute:

enctype="multipart/form-data"

any solution?


You need to set it as a header:

$optional_headers = array(
  'Content-Type'=>'multipart/form-data'
);

When calling the function:

do_post_request('http://www.imageshack.us/upload_api.php', array(
   'Content-Type'=>'multipart/form-data'
));

EDIT: I think you also have to url_encode rather than doing addslashes.



do_post_request(
    'http://www.imageshack.us/upload_api.php',
    $data,
    'Content-Type: multipart/form-data')
);

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜