0

我正在将具有以下 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); 
} 
?>

提前致谢!

4

2 回答 2

1

尝试

$_FILES["file"]["tmp_name"]

相反,在您移动上传的文件之前,它会存储在临时位置

于 2012-08-23T14:20:33.337 回答
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); 

在这里测试并完美运行。

于 2014-01-30T02:06:24.850 回答