0

I was trying to upload image using curl put request, And was able to upload image if the image is stored in local directory. but the problem is that i have to upload image from another image url. could anyone help me.

    $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);

i have tried to change $localfile to

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

but it gave me following error.

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

Warning: filesize(): stat failed for https://help.optimizely.com/hc/en-us/article_attachments/200205465/1._Site-Wide_Addition_gmc_BEFORE.png in C:\xampp\htdocs\newPutMedia.php on line 30

Tariq Husain
  • 559
  • 5
  • 23
  • What version of curl are you using? The curl for PHP fix [here](https://github.com/php-curl-class/php-curl-class/commit/4cb1436) should of resolved that issue a long time ago. Trying taking a look at [this question](http://stackoverflow.com/questions/17622156/curl-script-will-not-download-images-instead-renders-junk]). The error that you are getting is very rare and there is little to no documentation on it. – Luke Brown Jan 22 '16 at 14:59

1 Answers1

0

Apparently this is a bug in PHP cURL. You can use the bug fix as mentioned here or you can do this :

  1. Copy the file from URL to your local directory :

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

  2. Use the new file stored in your server for cURL PUT request :

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

Since the bug does not exist for file reading from your server, the bug won't cause errors and this would perfectly work.

Subin
  • 3,445
  • 1
  • 34
  • 63