ファイルアップロード / サーバー(PHP) へ / cURL(PHP) / MSXML2.XMLHTTP( VBScript )


  PHP の機能を使用した簡単なアップロード




ファイルデータは、target キーワードで渡されます

  

<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>
  

以下実行結果
アップロードに成功しました
Array ( [target] => Array ( [name] => up_dat [type] => text/plain [tmp_name] => C:\Documents and Settings\lightbox\Local Settings\Temp\phpA.tmp [error] => 0 [size] => 15 ) )

以下対象ファイル
  

0123456789アイウエオ
  

以下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








  PHP から cURL でアップロード




  

<?
// **********************************************************
// ファイルのアップロード( 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 ...

curl_setopt($curl, CURLOPT_HTTPHEADER, array('Expect:'));が必ず必要です


関連する記事

VB.net : クライアントが送ったヘッダを表示するだけの HTTPサーバーU

  VBScript( WSH ) でアップロード

  

<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 ...






yahoo  google  MSDN  MSDN(us)  WinFAQ  Win Howto  tohoho  ie_DHTML  vector  wdic  辞書  天気 


[sh_web]
CCBot/2.0 (http://commoncrawl.org/faq/)
17/10/19 23:33:02
InfoBoard Version 1.00 : Language=Perl

BatchHelper COMprog CommonSpec Cprog CprogBase CprogSAMPLE CprogSTD CprogSTD2 CprogWinsock Cygwin GameScript HTML HTMLcss InstallShield InstallShieldFunc JScript JScriptSAMPLE Jsfuncs LLINK OldProg OracleGold OracleSilver PRO PRObrowser PROc PROconePOINT PROcontrol PROftpclient PROjscript PROmailer PROperl PROperlCHAT PROphp PROphpLesson PROphpLesson2 PROphpLesson3 PROphpfunction PROphpfunctionArray PROphpfunctionMisc PROphpfunctionString PROsql PROvb PROvbFunction PROvbString PROvbdbmtn PROvbonepoint PROwebapp PROwin1POINT PROwinSYSTEM PROwinYOROZU PROwindows ProjectBoard RealPHP ScriptAPP ScriptMaster VBRealtime Vsfuncs a1root access accreq adsi ajax amazon argus asp aspSample aspVarious aspdotnet aw2kinst cappvariety centura ckeyword classStyle cmaterial cmbin cmdbapp cmenum cmlang cmlistbox cmstd cmstdseed cmtxt cs daz3d db dbCommon dbaccess dnettool dos download flex2 flex3 flex4 framemtn framereq freeWorld freesoft gimp ginpro giodownload google hdml home hta htmlDom ie9svg install java javaSwing javascript jetsql jquery jsp jspTest jspVarious lightbox listasp listmsapi listmsie listmsiis listmsnt listmspatch listmsscript listmsvb listmsvc memo ms msde mysql netbeans oraPlsql oracle oracleWiper oraclehelper orafunc other panoramio pear perl personal pgdojo pgdojo_cal pgdojo_holiday pgdojo_idx pgdojo_ref pgdojo_req php phpVarious phpguide plsql postgres ps r205 realC realwebapp regex rgaki ruby rule sboard sc scprint scquest sdb sdbquest seesaa setup sh_Imagick sh_canvas sh_dotnet sh_google sh_tool sh_web shadowbox shgm shjquery shvbs shweb sjscript skadai skywalker smalltech sperl sqlq src systemdoc tcpip tegaki three toolbox twitter typeface usb useXML vb vbdb vbsfunc vbsguide vbsrc vpc wcsignup webanymind webappgen webclass webparts webtool webwsh win8 winofsql wmi work wp youtube