简体   繁体   English

Windows PHP cURL上传未发布文件

[英]Windows PHP cURL upload not POSTing a file

I am having a peculiar issue with cURL on XAMPP 5.6.8. 我在XAMPP 5.6.8上遇到cURL的特有问题。 Using the below code, I am not able to send a file that does exist at the path specified in $tempPath . 使用以下代码,我无法发送在$tempPath指定的路径中确实存在的文件。 I am thinking that the cURL library may be getting confused with my path that starts with c:\\ . 我认为cURL库可能与以c:\\开头的路径混淆了。

My file is found here: C:\\tempFolder\\r_4878.tmp 在这里找到我的文件: C:\\tempFolder\\r_4878.tmp

On the linux server, using the exact same code, this does work using /mnt/temp/ . 在linux服务器上,使用完全相同的代码,这确实可以通过/mnt/temp/ Why should there be a difference? 为什么要有区别?

What might be breaking here? 这里可能发生什么事?

Upload Code 上载验证码

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

Remote Server 远端伺服器

$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" );

Response 响应

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
        )

)

I think you are only posting file information .. actual data is not posted. 我认为您仅发布文件信息..实际数据未发布。 File data needs to be POSTed as multipart. 文件数据需要作为多部分发布。

To debug you may want to create a form and see how that works in Network Tab . 要进行调试,您可能需要创建一个表单,然后在Network Tab中查看其工作方式。 It will allow you see specifically how data is sent when using a browser.. Once you see that you will have accurate idea on how to POST the file data. 它将使您可以具体查看使用浏览器时数据的发送方式。一旦看到,您将对如何过帐文件数据有准确的了解。

You should look at Using curl to upload POST data with files 您应该查看使用curl上传带有文件的POST数据

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM