1

What i really want to do is:

  • User will upload to script from a html page
  • The script will login and return back the link

Well, all can be easily done with the help of curl and accepting file through multipart post.

But the problem here is that it will start uploading the file from server to another through curl after all upload is done :( So that will take a long process. I was wondering whether its possible to make it like as user uploads files, it also keep sending the data in chunks like downloading a file

I am not sure whether this is even possible with php. If not, any other way through which this can be made possible

1 Answer 1

4

You can do this using cURL:

// Open a stream so that we stream the image download
$localFile = $_FILES[$fileKey]['tmp_name'];

$stream = fopen($localFile, 'r');

// Create a curl handle to upload to the file server     
$ch = curl_init($fileServer);
// Send a PUT request
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'PUT');
// Let curl know that we are sending an entity body
curl_setopt($ch, CURLOPT_UPLOAD, true);
// Let curl know that we are using a chunked transfer encoding
curl_setopt($ch, CURLOPT_HTTPHEADER, array('Transfer-Encoding: chunked'));
// Use a callback to provide curl with data to transmit from the stream
curl_setopt($ch, CURLOPT_READFUNCTION, function($ch, $fd, $length) use ($stream) {
    return fread($stream, $length) ? '';
});

curl_exec($ch);
Sign up to request clarification or add additional context in comments.

2 Comments

Hi, thanks for that code. surely that will help. But what about when we are using the stream to be the file user is uploading? :s because that can't be opened before. eg: $_FILE['file']
hmm well, yeah, but wouldn't this take place once the whole file is uploaded from client's pc to server? thats why i think this won't be possible in php since the request is taken after whole file is uploaded and not in chunks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.