通过卷曲

问题描述:

发送的multipart/form-data的

我有以下的HTML表单:通过卷曲

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

当我提交它,如果我做的print_r($ _ FILES)我得到以下的输出:

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

) 

我想弄清楚的是,如何用相同的方式发送文件,只使用PHP,将两个不同的系统连接在一起。我可以修改发送脚本,但不能修改接收脚本,所以我需要以预期的格式发送数据。

有没有办法使用cURL来发布数据到这个脚本,这将导致$ _FILES类似的输出?我有我想要在服务器上发送的文件,我只需要弄清楚如何将它发布到接收脚本。

+0

好像你要使用PHP的卷曲上传文件..检查[这](http://*.com/questions/15200632/how-to-upload-file-using-curl-with-php)和[this](http://code.stephenmorley.org/php/sending-files-using-curl/)一次。 –

我可以用下面的代码来解决这个问题:

// 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);