Php 如何使用CURL将SQL或任何文本文件从Localhost发送到网站
我的本地计算机上有一个和SQL文件,我想把这个文件发送到另一台本地主机上,它就像我的网站一样。之后,我想执行这个文件,并更新我的网站数据库。我尝试使用CURL,但没有按预期工作。下面给出了我的发送方和接收方文件,我还有两个文件Sql_generator.php和Sql_execute.php(执行服务器端接收的Sql文件)。请告诉我哪里做错了,因为我在一台本地主机上运行这个演示,代码运行正常,但当我从一台机器到另一台机器使用它时,服务器端会生成空的sql文件。请帮忙 Sender.phpPhp 如何使用CURL将SQL或任何文本文件从Localhost发送到网站,php,curl,Php,Curl,我的本地计算机上有一个和SQL文件,我想把这个文件发送到另一台本地主机上,它就像我的网站一样。之后,我想执行这个文件,并更新我的网站数据库。我尝试使用CURL,但没有按预期工作。下面给出了我的发送方和接收方文件,我还有两个文件Sql_generator.php和Sql_execute.php(执行服务器端接收的Sql文件)。请告诉我哪里做错了,因为我在一台本地主机上运行这个演示,代码运行正常,但当我从一台机器到另一台机器使用它时,服务器端会生成空的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>
<?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";
}
?>
Receiver.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>
<?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";
}
?>
您要“发送”到服务器的文件不是作为普通php文件发送的。它实际上是在服务器请求时由您机器上的Web服务器进行处理的
您需要修改curl请求以实际发送php文件,而不是指向它的链接
快速的谷歌搜索给了我这篇可能有用的文章: