I have two domain, as example site1.loc and site2.loc. In site1.loc i have a php form file like 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 like this:
<?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 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?
Thanks before.
No code in this answer, just a work flow example that will bypass
curl
:file_get_contents('http://site2.loc/pullfile.php?f=filename&sec=checkcode')
call. The contents ofpullfile.php
will do just that, pull the file from "site 1" to "site 2".file_get_contents()
can be checked for an error return from the "site 2"pullfile.php
and on error, deal with it. No error, remove the temp file.&sec=checkcode
could be used to confirm the file has been uploaded successfully to "site 2". It could be an MD5 of the file or something else you come up with.Just an idea.
Edit: Some sample code to help make things clearer, maybe :]
And this will be the 'pullfile.php' on site 2
Curl upload files
http://php.net/manual/en/function.curl-setopt.php
Code snippet:
The way you are doing it right now won't work because your server does not send the file to the remote server but the local filename (from the user). Also to transfer a file via CURL you need to add a '@' before the name.
Then the remote script also has to get the file from the $_FILES variable and copy it somewhere with
move_uploaded_file
(PHP.net)Also a tip: check
$_FILES['c_pic']['error']
before doing this to prevent uploading garbage to the remote server.