2016-12-28 10 views
0

を経由してマルチパート/フォームデータを送信する私は、次のHTMLフォームがあります。私はそれを送信すると、私がしますprint_r($ _ FILES)を行う場合、私は次の出力を取得し、cURLの

<form enctype="multipart/form-data" id="upload_form" role="region" action="remoteUpload2.php?command=FileUpload&amp;type=Files&amp;currentFolder=%2F&amp;langCode=en&amp;hash=8e402b8b9927640d&amp;" method="POST" target="ckf_19"> 
<input name="upload" type="file"> 
<input value="Upload Selected File" type="submit"> 
<input name="cancel" value="Cancel" type="button"> 
</form> 

を:

Array 
(
    [upload] => Array 
     (
      [name] => logo.png 
      [type] => image/png 
      [tmp_name] => /tmp/phpvIFI0K 
      [error] => 0 
      [size] => 12201 
     ) 

) 

私が理解しようとしているのは、2つの異なるシステムを一緒に接続するためにPHPだけを使用して同じ方法でファイルを送信する方法です。送信スクリプトは変更できますが、受信スクリプトは変更できませんので、期待される形式でデータを送信する必要があります。

このスクリプトにデータを投稿して$ _FILESに類似した出力が得られる方法はありますか?私はサーバー上で送信したいファイルを持っていますが、受信スクリプトにPOSTする方法を理解する必要があります。

+0

あなたは、PHPのCURLを使用してファイルをアップロードするように思える...([この]をチェックしhttp://stackoverflow.com/questions/15200632/how-to-upload-file-with-curl-with-php)と[this](http://code.stephenmorley.org/php/sending-files-using-curl/)を一度使用してください。 –

答えて

0

私は、次のコードでこの問題を解決することができました:

// initialise the curl request 
$request = curl_init('http://www.example.com/connector.php'); 

// send a file 
curl_setopt($request, CURLOPT_POST, true); 
curl_setopt($request, CURLOPT_SAFE_UPLOAD, false); 
curl_setopt(
    $request, 
    CURLOPT_POSTFIELDS, 
    array(
     'file' => '@' . realpath('logo.png') . ';filename=logo-test.png'. ';type=image/png' 
    )); 

// output the response 
curl_setopt($request, CURLOPT_RETURNTRANSFER, true); 

if(curl_exec($request) === false) 
{ 
    echo 'Curl error: ' . curl_error($ch)."<br>"; 
} 
else 
{ 
    echo 'Operation completed without any errors<br>'; 
} 

// close the session 
curl_close($request);