I have two domains, for example site1.loc and site2.loc . In site1.loc , I have a php form file similar to this:
<?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'=>$c_name, 'field2'=>$c_phone, 'field3'=>$c_pic ); $postvars=''; $sep=''; foreach($fields as $key=>$value) { $postvars.= $sep.urlencode($key).'='.urlencode($value); $sep='&'; } //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 ' <form action="" method="post" enctype="multipart/form-data"> Name : <input type="text" name="c_name" value="'.$c_name.'" /> <br /> Phone : <input type="text" name="c_phone" value="'.$c_phone.'" /> <br /> Image : <input type="file" name="c_pic" /> <br /> <input type="submit" /> </form> '; ?>
and handler.php in site2.loc as follows:
<?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"; } } ?>
This script works well for storing data in a database, but cannot load an image file. Can someone help me modify the scripts above to upload an image file?
Thanks before.
source share