我将一个带有以下html代码的文件提交到php文件。我想要做的是在php中获取该文件,然后将文件设置为cURL的参数值作为postfields然后执行url,我该怎么做?
这是html:
<form name="frm" id="frm" method="post" action="fileSubmit.php" enctype="multipart/form-data">
<input type="file" name="file" id="file"/>
<input type="submit" name="submit" value="submit" />
</form>
这是php
<?php
if(isset($_REQUEST['submit'])) {
$curl = curl_init("myDomain/submitFile";);
$file = "file=".file_get_contents($_FILES["file"]["name"]);
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, 0);
curl_setopt($curl, CURLOPT_TIMEOUT, 60);
curl_setopt($curl, CURLOPT_POST, 0);
curl_setopt($curl, CURLOPT_POSTFIELDS, $file);
$resp = curl_exec($curl);
curl_close($curl);
}
?>
提前致谢!
答案 0 :(得分:1)
试
$_FILES["file"]["tmp_name"]
相反,在您移动上传的文件之前,它会存储在临时位置
答案 1 :(得分:1)
这里有几件事需要修理...... 将您的代码更改为:
$postParams = array(
"@file" => $_FILES["file"]["tmp_name"],
"name" => $_FILES["file"]["name"]
);
$curl = curl_init("http://remote-site.com/upload-script.php");
curl_setopt($curl, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($curl, CURLOPT_HEADER, 0);
// curl_setopt($curl, CURLOPT_TIMEOUT, 60); // yes, this must be commented out. It will stop the upload otherwise.
curl_setopt($curl, CURLOPT_POST, 1); // post must be enabled.
curl_setopt($curl, CURLOPT_POSTFIELDS, $postParams);
$resp = curl_exec($curl);
curl_close($curl);
在这里进行测试,效果很好。