2

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 i.e. "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?

Benjamin Powers
  • 3,186
  • 2
  • 18
  • 23
  • You cannot use curl to have one remote server talk to another, unless either of those remote servers exposes some functionality to do so. – Marc B Oct 24 '11 at 17:22
  • @MarcB Why wouldn't it work if he used the temporary location of which the file was uploaded as the path to the cURL script? – John Cartwright Oct 24 '11 at 17:27
  • @john: OP's on box A, telling server B to send a file directly to server C, without involving box A. That can't be done via a curl call on box A unless server B has a script/service that allows for such things. – Marc B Oct 24 '11 at 17:34
  • @Brad I'm looking to upload a video to facebook, but my videos are on a separate box from my uploader. So my app in the middle would just be a redirection of sorts. (FB's upload uses just a simple post using multipart) – Benjamin Powers Oct 24 '11 at 19:44

2 Answers2

1

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

Community
  • 1
  • 1
Brad
  • 159,648
  • 54
  • 349
  • 530
0

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.

DarthVader
  • 52,984
  • 76
  • 209
  • 300