3

I want to use a php proxy to resolve the cross domain problem during uploading file. In the script i used php curl to forward request and response, but i don't know how to forward the request body to the server when the request body is multipart/format-data. file_get_contents('php://input') will be null when the Content-Type header is "multipart/form-data". And when using curl_setopt( $ch, CURLOPT_POSTFIELDS, $_POST), the Content-Disposition in request body could be sent, while the real data of file could not.

My request body is like this:

------WebKitFormBoundaryd0tvwIvevPV4VTlP
Content-Disposition: form-data; name="name"

nbzdds.mp3
------WebKitFormBoundaryd0tvwIvevPV4VTlP
Content-Disposition: form-data; name="chunk"

15
------WebKitFormBoundaryd0tvwIvevPV4VTlP
Content-Disposition: form-data; name="chunks"

44

497161
------WebKitFormBoundaryd0tvwIvevPV4VTlP
Content-Disposition: form-data; name="file"; filename="blob"
Content-Type: application/octet-stream

��v\;"S�;-�mG\u#...
.
.
.
3
  • If it's an option for you, it'd be much easier to upload the file locally on the same server as this php script, then curl the remote server with a URI to the uploaded file on the local server, then the remote server can download the file.
    – wyqydsyq
    Dec 13, 2011 at 4:17
  • Actually the file will be supplied by clients , using 'file' element of html. Did you mean i should cache the file on my script server first?
    – Hyden
    Dec 13, 2011 at 6:30
  • Yeah, rather than having the upload script try to upload it to the remote server, make the script store the file locally, then it should trigger a download script on the storage server. You could also store the file locally, then use curl's FTP function to upload the file from the script server to the storage server, this method would be ideal as you can have your script then delete the local cached copy of the file after it's been sent to the storage server.
    – wyqydsyq
    Dec 15, 2011 at 5:17

1 Answer 1

0

Ideally you would want to store the uploaded file locally, use curl's FTP function to upload the file from the script server to the storage server, then delete the cached file on the script server.

If this isn't possible, send a curl request to a script on the remote fileserver, which should download the file from the original server to itself.

It might not be quite as fast as sending the uploaded files directly to the remote fileserver, but it's essentially the same except you're caching the file locally while it uploads to the remote server. This might be better anyway because the uploaded data won't be stored in your server's ram for the entire operation, which could have lead to out of memory problems if lots of files were being uploaded simultaneously.

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.