title: PHP - Upload an image file to other domain with CURL
body: <p>I have two domain, as example <strong>site1.loc</strong> and <strong>site2.loc</strong>. In <strong>site1.loc</strong> i have a php form file like this:</p>
  
  <pre><code>&lt;?php
  $c_name = "";
  $c_phone = "";
  
  if($_SERVER['REQUEST_METHOD']=="POST"){
  
      $c_name = $_POST['c_name'];
      $c_phone = $_POST['c_phone'];
      $c_pic = $_FILES['c_pic']['name']; // Image file
  
          // submit target URL
          $url = 'http://site2.loc/handler.php';
  
          $fields = array(
             'field1'=&gt;$c_name,
             'field2'=&gt;$c_phone,
             'field3'=&gt;$c_pic
          );
  
          $postvars='';
          $sep='';
          foreach($fields as $key=&gt;$value) 
          { 
             $postvars.= $sep.urlencode($key).'='.urlencode($value); 
             $sep='&amp;'; 
          }
  
  
          //open connection
          $ch = curl_init();
  
          //set the url, number of POST vars, POST data
          curl_setopt($ch,CURLOPT_URL,$url);
          curl_setopt($ch,CURLOPT_POST,count($fields));
          curl_setopt($ch,CURLOPT_POSTFIELDS,$postvars);
  
          //execute post
          $result = curl_exec($ch);
  
          if(curl_errno($ch)) {
              echo 'Error: ' . curl_error($ch);
          }
          else {
              echo $result;
          }
  
          //close connection
          curl_close($ch);
  
  }
  
  
  echo '
  &lt;form action="" method="post" enctype="multipart/form-data"&gt;
      Name : &lt;input type="text" name="c_name" value="'.$c_name.'" /&gt; &lt;br /&gt;
      Phone : &lt;input type="text" name="c_phone" value="'.$c_phone.'" /&gt; &lt;br /&gt;
      Image : &lt;input type="file" name="c_pic" /&gt; &lt;br /&gt;
      &lt;input type="submit" /&gt;
  &lt;/form&gt;
      ';
  ?&gt;
  </code></pre>
  
  <p>and <strong>handler.php</strong> in <strong>site2.loc</strong> like this:</p>
  
  <pre><code>&lt;?php
  ob_start();
  if (!isset($_SESSION)) { session_start(); }
  
      // CONNECT TO DB
      $db_con = mysql_connect("localhost", "root", "root");// or die("Could not connect to db.");
      if(!mysql_select_db("site2",$db_con)) die("No database selected.");
  
      // POST
      if(isset($_POST)){
          $c_name = $_POST['field1'];
          $c_phone = $_POST['field2'];    
          $c_pic = $_POST['field3'];  
  
          // UPLOAD FILE
          /* UPLOAD IMAGE CODE HERE */
  
          // INSERT TO DB
          if(mysql_query("INSERT INTO kontak (nama, telpon) VALUES ('$c_name','$c_phone')")){
              echo "INSERT SUCCESS";
          } else {
              echo "INSERT FAILED";
          }
      }
  
  ?&gt;
  </code></pre>
  
  <p>This script runs well for storing the data to the database, but can not for upload an image file. Can anybody help me modify scripts above in order to upload an image file?</p>
  
  <p>Thanks before.</p>