2011-05-17 65 views
3

我們想象那個形式;上傳文件到服務器而不使用表單?

<form action="http://api.blabla.com/huhu.php" method="post" enctype="multipart/form-data"> 
     <input type="file" name="files[]" /> 
     <button type="submit">submit</button> 
    </form> 

我想上傳文件到此服務器而不使用上面的表單。

我試過這個與PHP捲曲,但我不能。

我想要它,因爲我有非常大量的文件上傳。這應該是自動與cron作業。

+0

您可以使用file_get_contents。 參見:[http://stackoverflow.com/questions/4003989/upload-a-file-using-file-get-contents][1] [1]:HTTP:// stackoverflow.com/questions/4003989/upload-a-file-using-file-get-contents – Macbric 2013-09-12 14:05:49

回答

5

這是你可以用開始捲曲文件上傳的例子:

$ch = curl_init('http://api.blabla.com/huhu.php'); 
curl_setopt_array($ch, array(
    CURLOPT_POSTFIELDS => array(
     'files[]' => '@/path/to/file', 
    ), 
)); 
if (false === ($res = curl_exec($ch))) { 
    die("Upload failed: " . curl_error($ch)); 
} 

字符串'@/path/to/file'有特殊的意義,因爲它與@開始;直接跟隨它的字符串應該包含您希望上傳的文件的路徑。