2012-08-23 55 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); 

在這裏測試和完美的作品。