2010-07-15 57 views
1

我有一個這樣的形式:使用PHP和捲曲後的HTML表單,包括文件

<form method="POST" action="i.php" enctype="multipart/form-data"> 
<input type="text" name="field1"> 
<input type="text" name="field2"> 
<input type="file" name="file"> 
<input type="hidden" name="MAX_FILE_SIZE" value="100000"> 
<input type="submit"> 
</form> 

在網頁上我已經有了:

$URL = 'http://somewhere.com/catch.php'; 
$fields = array('field1'=>urlencode($_POST['field1'), 'field2'=>urlencode($_POST['field2')); 

    foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }; 

    rtrim($fields_string,'&'); 
    $ch = curl_init(); 

    curl_setopt($ch,CURLOPT_URL,$URL); 
    curl_setopt($ch,CURLOPT_POST,count($fields)); 
    curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string); 

    $result = curl_exec($ch); 

    curl_close($ch); 

,致力於發佈FIELD1 & 2個字段。 是否有可能在某個捲曲過程中包含該文件?我會怎麼做?我假設我必須做的不僅僅是對文件值進行編碼。

因此,基於SimpleCoders的回答我更新以下內容:

$URL = 'http://somewhere.com/catch.php'; 
$fields = array('field1'=>urlencode($_POST['field1'), 'field2'=>urlencode($_POST['field2'), 'files'=>'@'. $_FILES['file']['tmp_name']); 

    foreach($fields as $key=>$value) { $fields_string .= $key.'='.$value.'&'; }; 

    rtrim($fields_string,'&'); 
    $ch = curl_init(); 

    curl_setopt($ch,CURLOPT_URL,$URL); 
    curl_setopt($ch,CURLOPT_POST, 1); 
    curl_setopt($ch,CURLOPT_POSTFIELDS,$fields_string); 

    $result = curl_exec($ch); 

    curl_close($ch); 

哪些職位確定,但隨後在catch.php我產生$ _FILES數組爲空。在http://www.php.net/manual/en/function.curl-setopt.php上註冊到示例#2這應該起作用。

我正在做這兩個不同的領域......可能是一個問題?

回答

2

嘗試這樣的事情,採取from here

$file_to_upload = array('file_contents'=>'@'.$file_name_with_full_path); 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_URL,$target_url); 
curl_setopt($ch, CURLOPT_POST,1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $file_to_upload); 
$result=curl_exec ($ch); 
curl_close ($ch); 
echo $result; 

還要考慮https://secure.php.net/manual/en/class.curlfile.php,這似乎是這樣做的更簡單,更現代的方式。

+0

由於無法保證外部鏈接的生命,您是否可以在此處發佈信息並保留參考? – 2016-04-20 18:56:34

+1

@感謝:確定的事情,完成 – 2016-04-20 20:46:47

+0

社區和那些來,謝謝。 :) – 2016-04-21 00:34:24

1

您需要按照SimpleCoders答案,但之後又改變了

$fields_string 

只是

$fields. 

http://www.php.net/manual/en/function.curl-setopt.php

注:將數組傳遞給CURLOPT_POSTFIELDS將編碼數據作爲多/ form-data,同時傳遞一個URL編碼的字符串會將數據編碼爲application/x-www-form-urlencoded。