我在使用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" }
编辑#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接收是他们回答的一个问题:)
答案 0 :(得分:1)
如果您想获取有关上传文件的信息,
print_r($_POST);
没用。您可能还想查看$_FILES
:
print_r($_FILES);
这是包含有关文件上传信息的变量。请read the manual about uploading files。