繁体   English   中英

无法通过php上传文件

[英]having trouble uploading a file via php

我将一个带有以下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); 
} 
?>

提前致谢!

尝试

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

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

这里有几件事需要修复......将代码更改为:

$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); 

在这里测试并且完美地工作。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM