How to send files in PHP using CURL
File uploads are very common these days. People upload their family photos or videos to show the world how awesome they are. Photos and videos take spaces and hard drives have their limits. A common way to fix this issue is to have storage servers.
If your farm is not setup with NFS, you will need some kind of way to upload them to a specific server. Here's a simple script that might be helpful if you need to send files using CURL to another server.
// http://www.domain.com/upload.php $ch = curl_init(); curl_setopt($ch, CURLOPT_HEADER, 0); curl_setopt($ch, CURLOPT_VERBOSE, 0); curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); curl_setopt($ch, CURLOPT_HTTPHEADER, array("Content-type: multipart/form-data")) curl_setopt($ch, CURLOPT_USERAGENT, "Mozilla/5.0 (X11; Linux i686; rv:6.0) Gecko/20100101 Firefox/6.0Mozilla/4.0 (compatible;)"); curl_setopt($ch, CURLOPT_URL, 'http://server101.domain.com/uploadfiles.php'); curl_setopt($ch, CURLOPT_POST, true); $post = array( 'file_1' => "@/tmp/uploads/file_1.jpg", 'file_2' => "@/tmp/uploads/file_2.png", 'name_1' => 'Image1', 'name_2' => 'Image2', 'username' => 'bookofzeus', ); curl_setopt($ch, CURLOPT_POSTFIELDS, $post); $response = curl_exec($ch); if(curl_errno($ch)) { echo 'Error: ' . curl_error($ch); } else { echo $response; }
Now the server101 will read the data sent by http://www.domain.com/upload.php
and saves it locally using $_FILES and $_POST.