[English Tutorial] Upload multiple files using php curl library

This one took a while for me to figure out. Thing is that no matter what I did it won’t upload a file. In other words php curl script on Server A wouldn’t send file contents to my upload.php script locate on server B.

As we all know that we have to set our form encoding type to enctype=”multipart/form-data” so that the file contents are sent with the post. That’s typical HTML way of doing things but we sometimes need to write a script that will do this from command line.

Well this is exactly what we will look at now

Here is a quick PHP snippet to upload a single file form Server A to Server B

The PHP

Here is a snippet to upload single file using curl library in php

 

At the server side

So at the server side (upload.php) just try this


The result of a demo post for me yielded this


 

Now we know how to upload a single file. What about multiple files in one go

Nothing fancy just usual stuff. Add another File in the upload array as shown below


So the above will yield this result

 

Ok so this turn out to be quite simple.

Note that we are using sign (@)  with file path. This is the trick with sending files to destination script.

Read section 4.3 in this document

http://curl.haxx.se/docs/httpscripting.html

 

I hope this helps. If you see any error in the code above please let me know so that I can fix it.

PHP curl library reference: http://www.php.net/manual/en/book.curl.php

Comments

comments

Share this Post :

No comments yet.

Please Post Your Comments & Reviews

Your email address will not be published. Required fields are marked *