2016-01-22 64 views
0

我試圖使用捲曲放置請求上傳圖片,並且能夠上傳圖片,如果圖片存儲在本地目錄中。但問題是我必須從另一個圖片網址上傳圖片。 任何人都可以幫助我。上傳帶有URL捲曲請求的圖片

$localfile = "testing.jpg"; 
// echo filesize($localfile); die; 
$url = API_URL . "pages/343/files/=".$localfile."?description=testing"; 

$fp = fopen ($localfile, "r"); 
$ch = curl_init(); 
curl_setopt($ch, CURLOPT_VERBOSE, 1); 
curl_setopt($ch, CURLOPT_USERPWD, API_USERNAME . ":" . API_PASSWORD); 
curl_setopt($ch, CURLOPT_HTTPAUTH, CURLAUTH_BASIC); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, false); 
curl_setopt($ch, CURLOPT_URL, $url); 
curl_setopt($ch, CURLOPT_PUT, 1); 
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1); 
curl_setopt($ch, CURLOPT_INFILE, $fp); 
curl_setopt($ch, CURLOPT_INFILESIZE, filesize($localfile)); 

$http_result = curl_exec($ch); 
$error = curl_error($ch); 
$http_code = curl_getinfo($ch ,CURLINFO_HTTP_CODE); 
// $jsonOutput = json_decode($http_result, true); 
curl_close($ch); 
fclose($fp); 

我試圖$ LOCALFILE改變

$localfile = "https://someurl/someimage.png"; 

,但它給了我下面的錯誤。

Warning: curl_setopt(): cannot represent a stream of type tcp_socket/ssl as a STDIO FILE* in C:\xampp\htdocs\newPutMedia.php on line 29 

警告:文件大小():STAT失敗https://help.optimizely.com/hc/en-us/article_attachments/200205465/1._Site-Wide_Addition_gmc_BEFORE.png在C:\ XAMPP \ htdocs中\ newPutMedia.php在線路30上

+0

你使用的是什麼版本的捲曲? PHP修復curl [這裏](https://github.com/php-curl-class/php-curl-class/commit/4cb1436)應該很久以前解決這個問題。試着看看[這個問題](http://stackoverflow.com/questions/17622156/curl-script-will-not-download-images-instead-renders-junk])。你得到的錯誤非常罕見,並且幾乎沒有任何文檔。 – LukeXF

回答

0

顯然這是一個bug in PHP cURL。你可以使用bug修復作爲mentioned here或者你可以這樣做:

  1. 複製從URL文件到本地目錄:

    file_put_contents(__DIR_\_ . "/testing.jpg", file_get_contents($url));

  2. 使用存儲在服務器上爲新文件捲曲PUT請求:

    $localfile = __DIR_\_ . "/testing.jpg";

由於從您的服務器讀取文件時不存在該錯誤,因此該錯誤不會導致錯誤,這完全有效。