2013-04-03 187 views
-1

我的圖片上傳使用下面的代碼ENCTYPE捲曲PHP

<form target="my_iframe" action="http://imagezilla.net/api.php" method="post" enctype="multipart/form-data"> 
    <input type="file" name="file" accept="image/x-png, image/gif, image/jpeg" /> 
    <input type="hidden" name="apikey" value="" /> 
    <input type="hidden" name="testmode" value="1" /> 
    <input type="submit" value="Upload Image" /> 
</form> 

這工作很好,但我沒有得到結果返回由於跨域規則的方式來imagezilla.net,所以我試圖把它變成捲曲PHP

<?php 
$ch = curl_init("http://imagezilla.net/api.php?file='C:\Anti-Backlash-Nut.jpg'&apikey=''&testmode=1"); 
$header = array('Content-Type: multipart/form-data'); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
$output = curl_exec($ch);  
curl_close($ch); 
echo $output; 
?> 

(第二批的代碼已經只是封閉的快速測試文件)
我不能在PHP代碼的MIME類型工作正確(行$ header = ...),因爲它只是沒有文件上傳。我究竟做錯了什麼?

回答

0

您正在使用GET方法上載文件...但文件始終得到使用後.. 對於你必須把@前上傳您要發送的文件名稱。

文件路徑之前發送@確保了捲曲的文件發送爲「多/表單數據」後的部分

試試這個

<?php 
$ch = curl_init("http://imagezilla.net/api.php?apikey=''&testmode=1"); 
$post = array(
    "file"=>"@C:\Anti-Backlash-Nut.jpg", 
); 
curl_setopt($ch, CURLOPT_HTTPHEADER, $header); 
curl_setopt($ch, CURLOPT_POST, 1); 
curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
$output = curl_exec($ch);  
curl_close($ch); 
echo $output; 
?> 
+0

我不得不將CURLOPT_HTTPHEADER,$標題更改爲CURLOPT_HEADER, 0,因爲它回來了一個錯誤,但現在當其成功輸出爲空時,如果有問題,則顯示結果 – user170324

+0

@ user170324這可能是因爲api可能不會返回任何成功上傳 – alwaysLearn

+0

當我使用表單方法時,它會返回圖像的服務器文件路徑,但它確實將它作爲xml返回,是否需要將標題更改回httpheader並將內容類型添加到xml? – user170324

1

這是你如何上傳文件:

<?php 
    $ch = curl_init(); 
    curl_setopt($ch, CURLOPT_URL, $url); 
    curl_setopt($ch, CURLOPT_POST, true); 
    $post = array(
     "file"=>"@/path/to/myfile.jpg", 
    ); 
    curl_setopt($ch, CURLOPT_POSTFIELDS, $post); 
    $response = curl_exec($ch); 
?> 
+0

如果我沒」我會使用此代碼t限制在我自己的服務器上的帶寬,因此,爲什麼我使用imagezilla,感謝您的意見,將它作爲一個儲備,當這不是一個問題 – user170324