我在本地计算机上有SQL文件,我希望将此文件发送到我的另一台localhost计算机,该计算机就像我的网站一样。之后,我想执行此文件并更新我的网站的数据库。我尝试使用CURL,但没有按预期工作。我的发送器和接收器文件在下面给出,我还有两个文件Sql_generator.php和sql_execute.php(这是执行在服务器端收到的sql文件)。请告诉我我在哪里做错了,因为我在单个localhost机器上运行此演示,其中代码工作正常但是当我从一台机器使用它到另一台机器时,则在服务器端生成空的sql文件。请帮忙 !!
Sender.php
<?php
include('sqlGenerator.php');
include('connect.php');
if(isset($_POST['sendfile'])){
// Creating a Back Up from Client-Side-Database
$return = backup_tables($name);
if($return){
echo "</br> <b>Back Up!! </b>is created successfully. </br></br>";
//clean your post here
$path = $_POST['db_file'];
$post = "file_address=".$path;
//Where are you sending it to?
$url = "http://192.168.1.21/final2/filereceiver.php";
//echo $post;
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_POST, true);
curl_setopt($ch, CURLOPT_POSTFIELDS, $post);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$response = curl_exec($ch);
curl_close($ch);
if($response){
echo "</br> <b>Congratulation!! </b> Your File has been Send Successfully </br></br>";
echo $response;
}
else{ echo "File not sent"; }
}
} ?>
<form action="sendfile.php" method="post"/>
<input type="file" name="db_file" value="http://192.168.1.20/xampp/htdocs/final/db.sql" readOnly="true"/>
<input type="submit" name="sendfile" value="Send File To Server"/>
</form>
Receiver.php
<?php
include("execute_sql.php");
class Download extends Executer{
const URL_MAX_LENGTH = 2050;
//clean url
protected function cleanUrl($url){
if(isset($url)){
if(!empty($url)){
if(strlen($url) < self::URL_MAX_LENGTH){
return strip_tags($url);
}
}
}
}
//is url
protected function isUrl($url){
$url = $this->cleanUrl($url);
if(isset($url)){
if(filter_var($url, FILTER_VALIDATE_URL, FILTER_FLAG_PATH_REQUIRED)){
return $url;
}
}
}
// return extension
protected function returnExtension ($url){
if($this->isUrl($url)){
$end = end(preg_split("/[.]/", $url));
if(isset($end)){
return $end;
}
}
}
//Download file
public function downloadFile($url){
if($this->isUrl($url)){
$extension = $this->returnExtension($url);
if($extension){
// curl Session
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$return = curl_exec($ch);
curl_close($ch);
$serverside_filename = "server_database_file";
$destination = "C:/xampp/htdocs/final/downloads/$serverside_filename.$extension";
$file = fopen($destination, "w+");
fputs($file, $return);
if(fclose($file)){
if(file_exists($destination)){
echo "<b>Server Says :- </b>Localhost send me a file successfully.. </br>
File is available at :- {$destination}</br></br>";
}
$path = "C:/xampp/htdocs/final/downloads/server_database_file.sql";
$obj1 = new Executer();
$res = $obj1->execute($path);
if($res){
echo "Your Data is inserted in Server's Database Successfully</br></br>";
}
else{
echo "Your Data Insertion is still Pending........</br> ";
}
}
}
}
} } ?>
<?php $obj = new Download();
if(isset($_POST['url'])){
$url = $_POST['url'];
}
// Receiving the file address from sender
if(isset($_POST['file_address'])){
$url = $_POST['file_address'];
if(isset($url)){
$obj->downloadFile($url);
}
}
else{
echo "</br>Sorry your file is not received";
}
?>
答案 0 :(得分:0)
你是的文件&#34;发送&#34;服务器不作为普通的php文件发送。当服务器请求时,它实际上是由您机器上的Web服务器处理的。
您需要修改curl请求以实际发送php文件而不是链接到它。
Google快速搜索给了我这篇可能有用的文章: