在 Windows 上使用 CURL、PHP 和 Apache 上传文件时出现问题

发布于 2024-12-08 07:07:12 字数 1197 浏览 0 评论 0原文

我在使用 CURL 和 PHP 上传文件时遇到问题(在 Windows 上 - 如果有问题的话)。

基本上,我可以发布到 URL 并显示已发布的内容(文件字段除外)。在这种情况下,我正在测试指向同一服务器的代码,但是一旦我确认文件已上传,我将发送到另一个域(同样,如果重要的话)。

这里是 start.php

$curl_handle=curl_init();
curl_setopt($curl_handle,CURLOPT_USERPWD, "user:pass");
curl_setopt($curl_handle,CURLOPT_URL, "http://domain/upload.php");
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl_handle,CURLOPT_POST, 1);
$post = array("boo"=>"yaaah",
        "xml"=>"@e:/path/to/file/new.xml");
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $post);
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);

然后, upload.php

print_r($_POST);

CURL 调用的结果,$buffer 的输出:

array(1) { ["boo"]=> string(5) "yaaah" }

Edit #1

相反,我做了一个 print_r($_FILES) 并得到了这个:

array(1) { ["xml"]=> array(5) { ["name"]=> string(7) "new.xml" ["type"]=> string(24) "application/octet-stream" ["tmp_name"]=> string(26) "C:\WINDOWS\TEMP\php892.tmp" ["error"]=> int(0) ["size"]=> int(3733) } }

这证明了我的文件正在上传。为什么我尝试访问的 API 没有获取它,这是他们需要回答的问题:)

I'm having issues uploading files using CURL with PHP (on Windows - if it matters).

Basically, I can post to the URL and display what has been posted except for the field with the file. In this case, I'm testing code that points to the same server, but once I can confirm that the file gets uploaded, I will send to another domain (again, if it matters).

Here is start.php

$curl_handle=curl_init();
curl_setopt($curl_handle,CURLOPT_USERPWD, "user:pass");
curl_setopt($curl_handle,CURLOPT_URL, "http://domain/upload.php");
curl_setopt($curl_handle,CURLOPT_RETURNTRANSFER,1);
curl_setopt($curl_handle,CURLOPT_POST, 1);
$post = array("boo"=>"yaaah",
        "xml"=>"@e:/path/to/file/new.xml");
curl_setopt($curl_handle, CURLOPT_POSTFIELDS, $post);
$buffer = curl_exec($curl_handle);
curl_close($curl_handle);

Then, upload.php

print_r($_POST);

The result of the CURL call, the output of $buffer:

array(1) { ["boo"]=> string(5) "yaaah" }

Edit #1

Instead, I did an print_r($_FILES) and got this:

array(1) { ["xml"]=> array(5) { ["name"]=> string(7) "new.xml" ["type"]=> string(24) "application/octet-stream" ["tmp_name"]=> string(26) "C:\WINDOWS\TEMP\php892.tmp" ["error"]=> int(0) ["size"]=> int(3733) } }

This proves that my file is being uploaded. Why it isn't being picked up by the API I'm trying to access is a question for them to answer :)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

命硬 2024-12-15 07:07:12

如果您想获取有关上传文件的信息,

print_r($_POST);

则没有帮助。您可能需要查看 $_FILES以及:

print_r($_FILES);

这是包含有关文件上传信息的变量。请阅读有关上传文件的手册

If you want to get information about uploaded files,

print_r($_POST);

is not helpful. You might want to look into $_FILES as well:

print_r($_FILES);

That is the variable that contains information about the file uploads. Please read the manual about uploading files.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文