Windows PHP cURL上传不发布文件

时间:2015-06-23 20:57:55

标签: php windows curl

我对XAMPP 5.6.8上的cURL有一个特殊的问题。使用以下代码,我无法发送在$tempPath中指定的路径中存在的文件。我认为cURL库可能会与以c:\开头的路径混淆。

我的文件位于:C:\tempFolder\r_4878.tmp

在linux服务器上,使用完全相同的代码,这可以使用/mnt/temp/。为什么要有区别?

这可能会破坏什么?

上传代码

$post = array( 'file_name' => $reportID, 'file_contents'=>'@'.$tempPath.'' );

$return = true;

# set the url that we need to use to upload to each server
$url = "http://server.corp/uploadServer.php";

# curl the file to the remote server
$ch = curl_init( $url );
curl_setopt( $ch, CURLOPT_POST,             true    );
curl_setopt( $ch, CURLOPT_HEADER,           false   );
curl_setopt( $ch, CURLOPT_RETURNTRANSFER,   true    );
curl_setopt( $ch, CURLOPT_TIMEOUT,          240     );
curl_setopt( $ch, CURLOPT_POSTFIELDS,       $post   );
curl_setopt( $ch, CURLOPT_HTTPHEADER, array( 'Accept: application/json' ));

# get the servers respone and decode the json response
$result = json_decode( curl_exec( $ch ) );

$t = array( $url, $tempPath, file_exists( $tempPath ), $result->success, $post, $result );

echo "\r\n".print_r( $t, true )."\r\n\r\n";

curl_close( $ch );

远程服务器

$output['file_exists'] = file_exists( $_FILES["file_contents"]["tmp_name"] );
$output['file_path']   = $fileName.".txt";
$output['tmp_name']    = $_FILES["file_contents"]["tmp_name"];
$output['success']  = move_uploaded_file( $_FILES["file_contents"]["tmp_name"], $fileName.".txt" );

响应

Array
(
    [0] => http://server.corp/uploadServer.php
    [1] => C:\tempFolder\r_4878.tmp
    [2] => 1
    [3] => 
    [4] => Array
        (
            [file_name] => UnitTest25-felix
            [file_contents] => @C:\tempFolder\r_4878.tmp
        )

    [5] => stdClass Object
        (
            [file_name] => UnitTest25-felix
            [file_exists] => 
            [file_path] => UnitTest25-felix.txt
            [tmp_name] => 
            [success] => 
            [generationTime] => 9.70363616943E-5
        )

)

1 个答案:

答案 0 :(得分:2)

我认为您只发布文件信息..实际数据未发布。文件数据需要以多部分的形式发布。

要进行调试,您可能需要创建一个表单,并在“网络”选项卡中查看其工作原理。它将允许您具体了解使用浏览器时数据的发送方式。 一旦您看到您将准确了解如何发布文件数据。

您应该查看Using curl to upload POST data with files

相关问题