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('http://example.com/upload.php');

$URL = 'https://google.com/download/image.jpg';
$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);
url_exec($ch);

Is there a way? please guide me

1 answer

  • answered 2018-04-14 19:14 hanshenrik

    not with CURLFile, but with CURLOPT_INFILE, probably

    $fp=fopen($url,"rb");
    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