PHP CurlFile upload with URL

There is a way in PHP to upload a file with url? (Link from another server or site)
I do not want to download it locally, and then upload it again!
Is it possible with CurlFile?

for example:

$ch = curl_init('');

$URL = '';
$cfile = new CURLFile('$URL','image/jpeg','test_name');

$data = array('test_file' => $cfile);
curl_setopt($ch, CURLOPT_POST,1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $data);

Is there a way? please guide me

1 answer

  • answered 2018-04-14 19:14 hanshenrik

    not with CURLFile, but with CURLOPT_INFILE, probably

    curl_setopt_array ( $ch, array (
            CURLOPT_INFILE => $fp,
            CURLOPT_READFUNCTION => function ($ch, $fp, $max) {
                $ret = fread ( $fp, $max );
                if (! is_string ( $ret )) {
                    return "";
                return $ret;
    ) );
    • untested, but should work in theory. it will download and upload the file in pieces, as requested by libcurl, in-ram, and should at no point write anything to the harddrive