|
|
<html>
<head>
<meta HTTP-EQUIV="Content-Type" CONTENT="text/html; charset=shift_jis">
</head>
<body>
<form
enctype="multipart/form-data"
method="POST"
>
アップロードするファイル :
<input name="target" type="file" style='width:400px' size="65">
<input type="submit" value="アップロード">
</form>
<pre>
<?
if ( $_SERVER['REQUEST_METHOD'] == "POST" ) {
$upload = realpath( "./data" );
$upload .= ( DIRECTORY_SEPARATOR . $_FILES['target']['name'] );
if ( move_uploaded_file(
$_FILES['target']['tmp_name'], $upload ) ) {
print "アップロードに成功しました<br>\n";
}
print_r( $_FILES );
}
?>
</pre>
</body>
</html>
| |
|
以下実行結果
以下対象ファイル
以下IEとのやりとり
POST /web/upload/test.php HTTP/1.1
Accept: image/gif, image/jpeg, image/pjpeg, image/pjpeg, application/x-shockwave-flash, application/x-ms-application, application/x-ms-xbap, application/vnd.ms-xpsdocument, application/xaml+xml, application/vnd.ms-excel, */*
Referer: http://localhost/web/upload/test.php
Accept-Language: ja
User-Agent: Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)
Content-Type: multipart/form-data; boundary=---------------------------7daea24110370
Accept-Encoding: gzip, deflate
Host: localhost
Content-Length: 201
Connection: Keep-Alive
Cache-Control: no-cache
-----------------------------7daea24110370
Content-Disposition: form-data; name="target"; filename="up_dat"
Content-Type: text/plain
0123456789アイウエオ
-----------------------------7daea24110370--
HTTP/1.1 200 Document follows
MIME-Version: 1.0
Server: AnWeb/1.42p
Date: Mon, 28 Jun 2010 08:33:36 GMT
Transfer-Encoding: chunked
Expires: -1
Pragma: no-cache
Cache-control: no-cache
Last-Modified: Mon, 28 Jun 2010 08:33:36 GMT
X-Powered-By: PHP/5.3.0
Content-type: text/html
|
|
|
<?
// **********************************************************
// ファイルのアップロード( cURL による直接アップロード )
// **********************************************************
header( "Content-Type: text/html; Charset=shift_jis" );
header( "pragma: no-cache" );
header( "Expires: Wed, 31 May 2000 14:59:58 GMT" );
header( "Cache-control: no-cache" );
// *********************************************************
// curl 処理
// *********************************************************
$curl = curl_init();
curl_setopt($curl, CURLOPT_CONNECTTIMEOUT, 30);
curl_setopt($curl, CURLOPT_HEADER, false);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, true);
curl_setopt($curl, CURLOPT_BINARYTRANSFER, true);
curl_setopt($curl, CURLOPT_URL, "http://localhost/web/upload/test.php");
curl_setopt($curl, CURLOPT_POST, true);
// 対応していないサーバー用 ( ここでは、easy_server )
curl_setopt($curl, CURLOPT_HTTPHEADER, array('Expect:'));
$data = array( 'target' => '@' . realpath("./up_dat") );
curl_setopt($curl, CURLOPT_POSTFIELDS, $data );
// *********************************************************
// 送信
// *********************************************************
curl_setopt($curl, CURLOPT_VERBOSE, true); // デバッグ
$handle = fopen("./debug.txt", "w");
curl_setopt($curl, CURLOPT_STDERR, $handle);
$handle2 = fopen("./ret_header.txt", "w");
curl_setopt($curl, CURLOPT_WRITEHEADER, $handle2);
$result = curl_exec($curl);
// *********************************************************
// 結果
// *********************************************************
if($result === false) {
$result = 'Curl error: ' . curl_error($curl);
}
curl_close($curl);
fclose($handle2);
fclose($handle);
print( $result . "\n" );
?>
| |
|
dubug.txt の内容
* About to connect() to localhost port 80 (#0)
* Trying 127.0.0.1... * connected
* Connected to localhost (127.0.0.1) port 80 (#0)
> POST /web/upload/test.php HTTP/1.1
Host: localhost
Accept: */*
Content-Length: 215
Expect: 100-continue
Content-Type: multipart/form-data; boundary=----------------------------51208a50c44a
* Done waiting for 100-continue
< HTTP/1.1 200 Document follows
< MIME-Version: 1.0
< Server: AnWeb/1.42p
< Date: Mon, 28 Jun 2010 10:31:35 GMT
< Transfer-Encoding: chunked
< Expires: -1
< Pragma: no-cache
< Cache-control: no-cache
< Last-Modified: Mon, 28 Jun 2010 10:31:35 GMT
< X-Powered-By: PHP/5.3.0
< Content-type: text/html
<
* Connection #0 to host localhost left intact
* Closing connection #0
easy_server に送った場合
( easy_server )
port : 50000
Waiting ...
Connected
-----------------------------
== data length : 162 ==
POST / HTTP/1.1
Host: localhost:50000
Accept: */*
Content-Length: 215
Content-Type: multipart/form-data; boundary=----------------------------75c5ad27b289
== data length : 215 ==
------------------------------75c5ad27b289
Content-Disposition: form-data; name="target"; filename="up_dat"
Content-Type: application/octet-stream
0123456789?????
------------------------------75c5ad27b289--
== data end ==
Waiting ...
関連する記事
VB.net : クライアントが送ったヘッダを表示するだけの HTTPサーバーU |
|
|
<JOB>
<COMMENT>
************************************************************
URLEncode用
************************************************************
</COMMENT>
<OBJECT id="Stream" progid="ADODB.Stream" />
<OBJECT id="StreamWorkBin" progid="ADODB.Stream" />
<OBJECT id="StreamBin" progid="ADODB.Stream" />
<COMMENT>
************************************************************
HTTP通信用
************************************************************
</COMMENT>
<OBJECT id="objHTTP" progid="MSXML2.XMLHTTP" />
<SCRIPT language=VBScript>
' **********************************************************
' Twitter 投稿用 API URL
' **********************************************************
upload_url = "http://localhost/web/upload/test.php"
' *********************************************************
' API へ向けて送信準備
' *********************************************************
Call objHTTP.Open( "POST",upload_url, False )
' File Upload 用 HTTP ヘッダ
strBoundary = DateDiff("s", "1970/1/1 0:00:00",DateAdd("h",-9,now))
Call objHTTP.setRequestHeader("Content-Type", "multipart/form-data; boundary="&strBoundary)
' バイナリ変換用ストリーム
StreamWorkBin.Open
StreamWorkBin.Type = 1
' 最終バイナリストリーム
StreamBin.Open
StreamBin.Type = 1
' テキストストリーム
Stream.Open
Stream.Charset = "shift_jis"
' 開始セクション
Stream.WriteText "--" & strBoundary & vbLf
Stream.WriteText "Content-Disposition: form-data; name=""target""; filename=""up_dat""" & vbLf
Stream.WriteText "Content-Type: text/plain" & vbLf
Stream.WriteText vbLf
Stream.Position = 0
' テキストをバイナリに変換
Stream.CopyTo StreamWorkBin
' 第一セクションを書き込み
StreamWorkBin.Position = 0
StreamBin.Write StreamWorkBin.Read(StreamWorkBin.Size)
' 画像を読み込む
StreamWorkBin.LoadFromFile("up_dat")
' 画像を書き込み
StreamBin.Write StreamWorkBin.Read(StreamWorkBin.Size)
' バイナリワークをいったん閉じる
StreamWorkBin.Close
StreamWorkBin.Open
StreamWorkBin.Type = 1
' テキストをいったん閉じる
Stream.Close
Stream.Open
Stream.Charset = "shift_jis"
' 終了セクション
Stream.WriteText vbLf & "--" & strBoundary & "--" & vbLf
Stream.Position = 0
' テキストをバイナリに変換
Stream.CopyTo StreamWorkBin
' 終了セクションを書き込み
StreamWorkBin.Position = 0
StreamBin.Write StreamWorkBin.Read(StreamWorkBin.Size)
' 送信データを取得
nLen = StreamBin.Size
StreamBin.Position = 0
strData = StreamBin.Read(nLen)
Call StreamBin.SaveToFile( "result.dat", 2 )
' *********************************************************
' 投稿データとその長さ
' *********************************************************
Call objHTTP.SetRequestHeader("Content-Length",nLen)
' *********************************************************
' API へ向けて送信
' *********************************************************
Call objHTTP.Send(strData)
Wscript.Echo(objHTTP.responseText)
</SCRIPT>
</JOB>
| |
|
easy_server に送った場合
( easy_server )
port : 50000
Waiting ...
Connected
-----------------------------
== data length : 510 ==
POST / HTTP/1.1
Accept: */*
Accept-Language: ja
Content-Type: multipart/form-data; boundary=1277734245
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 5.1; Trident/4.0; .NET CLR 2.0.50727; .NET CLR 3.0.4506.2152; .NET CLR 3.5.30729)
Host: localhost:50000
Content-Length: 135
Connection: Keep-Alive
Cache-Control: no-cache
--1277734245
Content-Disposition: form-data; name="target"; filename="up_dat"
Content-Type: text/plain
0123456789?????
--1277734245--
== data end ==
Waiting ...
|
|