Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/229.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Can';我无法在PHP中通过Curl上传文件_Php_File_Curl_Upload - Fatal编程技术网

Can';我无法在PHP中通过Curl上传文件

Can';我无法在PHP中通过Curl上传文件,php,file,curl,upload,Php,File,Curl,Upload,我一直在搜索如何在PHP中通过curl上传文件。。。我发现您所需要做的就是使用绝对路径,并将“@”放在路径/文件名的前面,它应该可以工作。我的情况不是这样。以下是我发布文件的代码: $url = "http://my_url/testing.php"; $partner_key = "XXXX"; $secret_key = "YYYY"; $resume_file = realpath("resumes/$newfilename"); $first_name = $_REQUEST['app

我一直在搜索如何在PHP中通过curl上传文件。。。我发现您所需要做的就是使用绝对路径,并将“@”放在路径/文件名的前面,它应该可以工作。我的情况不是这样。以下是我发布文件的代码:

$url = "http://my_url/testing.php";

$partner_key = "XXXX";
$secret_key = "YYYY";
$resume_file = realpath("resumes/$newfilename");
$first_name = $_REQUEST['apply_firstname'];
$last_name = $_REQUEST['apply_lastname'];
$email = $_REQUEST['apply_email'];

$content  = "partner_key=$partner_key&";
$content .= "secret_key=$secret_key&";
$content .= "resume_file=@$resume_file&";
$content .= "first_name=$first_name&";
$content .= "last_name=$last_name&";
$content .= "email=$email";

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, false);
curl_setopt($ch, CURLINFO_HEADER_OUT, true);
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $content);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_VERBOSE, 1);
$response = curl_exec($ch);
curl_close($ch);

print $content . "<br>";
print $response;

有什么我遗漏的吗?也许是一个我没有设置的卷发器?

我终于弄明白了

为了让文件上传工作,我的帖子字段必须被构造成一个数组,而不仅仅是一个url字符串

所以我改变了这个:

$content  = "partner_key=$partner_key&";
$content .= "secret_key=$secret_key&";
$content .= "resume_file=@$resume_file&";
$content .= "first_name=$first_name&";
$content .= "last_name=$last_name&";
$content .= "email=$email";
为此:

$content = array(
  'partner_key' => $partner_key,
  'secret_key' => $secret_key,
  'resume_file' => "@" . $resume_file,
  'first_name' => $first_name,
  'last_name' => $last_name,
  'email' => $email
);

现在它工作得很好

问题在于“@”字符不是
$content
字符串中的第一个字符。此外,在PHP5.0中不赞成在postfields中使用“@”

解决方案是使用并让
$content
成为一个数组:

$curl_file_upload = new CURLFile($resume_file);
$content = array("partner_key" => $partner_key,
        "secret_key" => $secret_key,
        "resume_file" => $curl_file_upload,
        "first_name" => $first_name,
        "last_name" => $last_name,
        "email" => $email);
后来

curl_setopt($ch, CURLOPT_POSTFIELDS, $content);

希望这有帮助。

请插入
echo curl\u errno($ch)。“”.curl\u errtxt($ch)位于包含curl_close()的行之前。这应该给你一个错误的提示!谢谢你,迈克尔!我试过了,当curl\u errtxt($ch)破坏了我的脚本时,curl\u errno($ch)返回一个“0”,curl\u error($ch)什么也不返回。所以curl请求基本上成功了…对,但是我无法从接收脚本访问我的“上传文件”。我回显的第一个数组是$\u FILES数组,您可以看到它是空的。这可能是我的php.ini中不允许curl附加文件的设置吗?或者这根本不是一件事……:)谢谢你,迈克尔!我感谢你的帮助,你真是太棒了!
curl_setopt($ch, CURLOPT_POSTFIELDS, $content);