2012-11-18 14 views
0

我有一個疑問,如果我可以傳遞一個數組內容的文件到捲曲循環的鏈接,創建一個單一的循環,而不是每次我需要上傳文件的內容,拷貝並使用不同的文件名粘貼相同的捲曲調用。與列表文件傳遞數組捲曲循環

這裏是我寫的上傳到服務器的單個文件的代碼。我複製並粘貼相同的代碼爲每一個單一的文件,並以這種方式一切正常,但我想了解如何創建一個單一循環給予「$ data = array('file'=> $ fn);」循環包含所有文件內容,並在調用函數「file_get_contents($ fn)」時自動執行一個函數中的所有調用。

//file to upload 

$fn = ("fn.rdf"); 

//curl loop to upload single file 
    $data = array('file'=>$fn); 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/rdf+xml')); 
    curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
    curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/index.php?update=true'); 
    curl_setopt($ch, CURLOPT_POST, true); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents($fn)); 
    curl_exec($ch); 
    $info = curl_getinfo($ch); var_dump($info); 
    $result = curl_exec($ch); 
    if ($result === false) { 
     die(curl_error()); 
    }else{ 
     echo "<br>"."upload ok"."<br>"; 
    } 

謝謝!

編輯:嗨,我再次懷疑。如果我想使用該腳本作爲函數將其方法調用爲逐一讀取文章的另一個腳本,則在此我必須將function updateContent(){}調用到其他腳本的方法updateContent()? 正如我已經說過的,$ fn包含一個包含文件列表的數組。如果我要呼叫的updateContent()創建,但僅僅針對特定file1.rdf是正確使用該命令updateContent($files['0']);調用只爲元素0中指定的方法(對應於所提及的陣列的file1.rdf)來更新它的狀態?

回答

0

你是指那樣的事情?

$files = array('1.rdf', '2.rdf', '3.rdf', 'x.rdf', 'whatever.xyz'); 

$ch = curl_init(); 
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Content-Type: application/rdf+xml')); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); 
curl_setopt($ch, CURLOPT_URL, 'http://www.example.com/index.php?update=true'); 
curl_setopt($ch, CURLOPT_POST, true); 

foreach ($files as $fn) { 
    curl_setopt($ch, CURLOPT_POSTFIELDS, file_get_contents($fn)); 
    $result = curl_exec($ch); 
    $info = curl_getinfo($ch); 
    var_dump($info); 
    if ($result === false) { 
     die(curl_error()); 
    }else{ 
     echo "<br />".$fn." upload ok"."<br />"; 
    } 
} 
+0

GREAT!正是我需要了解我的錯誤。 非常感謝 – Bebbolin