2016-01-22 65 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";

由于从您的服务器读取文件时不存在该错误,因此该错误不会导致错误,这完全有效。