I have a form which allows users to upload large video files to a 3rd party API endpoint. The API expects the api key as one of the parameters, so in order to keep it protected, I submit the form to a PHP script and the PHP script sends the file to the API using curl. This works, but the problem is that it means the file gets uploaded twice: once to my php script, then again from the PHP script to the API. Is there a way to do the two in parallel? In other words, let the curl script start uploading BEFORE the ajax upload is complete?
If you change from post to put you could use
$input = fopen('php://input','r');
And then fread it likes standard large file and then send the partial chunks with curl ?
You will need to use CURLOPT_READFUNCTION to pass it in chunks.
Be aware if the user stops for what ever reason you will need to clean up the missing files
I'm not sure of a way using post
Small example
set curl up with this option
curl_setopt($ch, CURLOPT_READFUNCTION, functionCall));
function functionCall($ch, $data){
global $input;
return fread($input, $length);
}
$length is the amount of data sent in each chunk. The suer will have to upload at least that amount before the upload starts
8192 is a popular length, Give that a go