2011年03月

2011年03月02日

同僚がHTTP_Requestでファイルアップロードするのに一回ファイルに保存するのが気に食わないというので適当にかいた。
まぁ普通にmultipartのデータつくってstream_context_createでContent-Typeとか設定してるだけ。

<?php
$boundary = '---------------------------'.time();

$data = <<< __data
--{$boundary}
Content-Disposition: form-data; name="test"

hogehoge
--{$boundary}
Content-Disposition: form-data; name="test2"

foobar
--{$boundary}
Content-Disposition: form-data; name="file"; filename="test.txt"
Content-Type: text/plain

value2
--{$boundary}--
__data;

$header = array(
    "Content-Type: multipart/form-data; boundary=".$boundary,
    "Content-Length: ".strlen($data)
);

$context = array(
    "http" => array(
        "method"  => "POST",
        "header"  => implode("\r\n", $header),
        "content" => $data
    )
);
$ctx = stream_context_create($context);
$url = 'http://example.com/upload_url';
var_dump(file_get_contents($url,false,$ctx));



haruta_makotoharuta_makoto at 21:59│コメント(0)トラックバック(0)PHP │