0

I've got a website A that holds an image, and I am now using PHP to grab that image and I want to post it via form-data media to another website. My solution now is to save the image as a file and then use CURLFile to auto-generating these form-media post fields. Is there a way that I don't need to save this image to my local file system?

What I am doing now:

$img = urlcurl::geturl($this->url); // seems to be an raw image data (png?)
$fp = fopen('./tmp/1.png','x');
fwrite($fp, $img);
$t = new CURLFile("/tmp/1.png","image/png"); // get from local file storage
$filedata = array("media" => $t);

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url2);
curl_setopt($ch, CURLOPT_POSTFIELDS, $filedata);
$response = curl_exec($ch);
Mahmoud Abdelsattar
  • 1,299
  • 1
  • 15
  • 31
  • You could probably use a variation of [this](https://stackoverflow.com/a/46806512/231316), where in the callback you do your pull from the remote server. However I’d personally recommend against this because it is much harder to debug, especially if one or the other server gets interrupted. Instead, just save local to a [unique temporary path](https://stackoverflow.com/a/460168/231316) and your system should take care of pruning that eventually if you don’t manually delete it. – Chris Haas Sep 17 '22 at 15:16

0 Answers0