php - Windows PHP cURL 上传不发布文件

标签 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
        )

)

最佳答案

我认为您只是发布文件信息.. 实际数据并未发布。文件数据需要作为多部分发布。

要进行调试,您可能需要创建一个表单并在 Network Tab 中查看它是如何工作的。它将允许您具体查看使用浏览器时数据是如何发送的。 一旦您看到,您将准确了解如何发布文件数据。

你应该看看Using curl to upload POST data with files

关于php - Windows PHP cURL 上传不发布文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31013395/

相关文章:

php - FTP 使用 CURL 将文件上传到远程服务器,PHP 上传一个空白文件

php - 如何对具有重复值的数组值进行排序并在出现平局时跳过某些位置?

java - 如何将 Pact HTTP 请求交互转换为 Curl

c++ - curl 发布空对象

javascript - 从数据库中提取 Javascript

java - 在 Java 中获取当前的 Windows 键盘布局

windows - 使用 GREP 计算与模式匹配的文件

windows - 试图找出 MASM 语法

使用 OpenSSL 的 libcurl 的 Android-21 静态构建

php - ffmpeg 没有从 php exec() 函数运行,从命令行工作