我有两个域,例如site1.loc和site2.loc。在site1.loc我有一个这样的 php 表单文件:
<?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>
';
?>
和site2.loc中的handler.php如下所示:
<?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";
}
}
?>
该脚本运行良好,可以将数据存储到数据库,但不能上传图像文件。任何人都可以帮我修改上面的脚本以上传图像文件吗?
之前谢谢。