2012-08-23 57 views
0

我正在将包含以下html代码的文件提交给php文件。我想要做的是在php中获取该文件,然后将该文件设置为cURL的参数值作为postfields,然后执行url,我该怎么做?通过php上传文件时遇到问题

下面是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); 
} 
?> 

谢谢!

+1

什么问题?显示php代码 –

+1

请更清楚您的问题显示代码和问题在哪里? –

+0

这是我试图上传文件的php代码。我做错了什么? (isset($ _ REQUEST ['submit'])) <?php if(isset($ _ REQUEST ['submit'])) {0} $ 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); } ?> – seeker

回答

1

尝试

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

代替,直到你将上传文件时,它被保存在一个临时位置

+0

谢谢Gabriel, 有什么办法可以将文件内容作为流发送到PHP中。我看到了PHP流的东西,但是 – seeker

+0

它仍然不工作! – seeker

+0

问题是我正在通过从html页面向php代码发送一个文件。我想发送文件,如https:// myDomain/fileSubmit?file = <从HTML代码上传的文件>。任何人都可以想出如何做到这一点? – seeker

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

在这里测试和完美的作品。