简体   繁体   中英

cURL php download and upload without save in the middle

I have a situation where I need to move files from one location to another. Using cURL, it is simple to download the files and then upload them using the sweet at symbol ie "file_box"=>"@/path/to/myfile.jpg" What I'm looking to do now is bypass the saving step in the middle and just "stream" the file from one to the other. Is this possible?

Sure, you can do that. I'm not sure you can use cURL for the POST though. You'd have to look into its methods to see if it will let you callback for the next chunk of data.

http://curl.haxx.se/libcurl/php/examples/callbacks.html

$ch = curl_init();
curl_set_opt($ch, CURLOPT_HEADERFUNCTION, 'read_header');
curl_set_opt($ch, CURLOPT_WRITEFUNCTION, 'read_body');

function read_header($ch, $data) {
    // Ok, so the header is going to come in here.
    // I assume you will need data, such as MIME type and what not.
    print_r($data);
    return strlen($data); //This means that we handled it, so cURL will keep processing
}

function read_body($ch, $data) {
    // This is where the body of the content will be, in chunks.
    // This function will be called multiple times.
    print_r($data);
    return strlen($data); //Again, if we don't do this, cURL will cancel.
}

Now for the sending part, it seems you will have to implement HTTP yourself with fsockopen . See here for more information: PHP How To Send Raw HTTP Packet

I m actually working with a similar problem now. and as far as my experience, you will have to open a file handle, and save the data to local before being able to upload it to another server.

I know what you mean by streaming, as you want to stream/pipe the file you are downloading to PUT or POST to another server.

I dont think streaming is possible with Php curl. You need save the file locally. then open it and upload it.

I have done a similar job with Java, with that you are actually using streams, so that might work but I havent tried it.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM