2016-07-29 69 views
0

嗨我需要創建一些自動連接到另一臺服務器使用PHP庫,然後我需要加載數據在MySQL數據庫中只有第一個文件上傳一個新文件是每天上傳。問題是我怎麼繼續進行日常文件上傳到數據庫中,我幾乎沒有如何從另一個服務器每天自動加載數據

下面是代碼:

<?php 
include 'core/init.php'; 
include 'includes/overall/header.php'; 

//connection to linux server 
$conn = ssh2_connect('xxx.xxx.xx.xxx', 22); 
$destinationPath = '/path/to/destination/path/'; 
$localPath = 'C:\path\to\local\path\'; 

//checks if the connection is successful or not 
if(ssh2_auth_password($conn, 'username', 'password')){ 
echo '<script type="text/javascript">alert("Authentication was successful"); </script>'; //javascript pop up when successful 
    }else{ 
    die("Authentication failed"); 
} 

if(ssh2_scp_recv($conn, $destinationPath, $localPath)){ 
echo '<h2>Todays file recieved</h2>'; //if file was recieved from server to local echo todays file recieved, putting the file in localpath 
}else{ //if the file was not uploaded send an email for radar file too be uploaded 

$to = '[email protected]'; 
$subject = 'the subject'; 
$message = 'hello'; 
$headers = "From: The Sender Name <[email protected]>\r\n"; 
$headers .= "Reply-To: [email protected]\r\n"; 
$headers .= "Content-type: text/html\r\n"; 
mail($to, $subject, $message, $headers); 

}

$string = file_get_contents('http://localhost/Prototype/core/edit.txt', 'r');//get contents of file from web used to read the file 
$myFile = 'C:wampwwwPrototypecoreedit.txt';//file directory 
$fh = fopen($myFile, 'w') or die("Could not open: " .mysql_error());//open the file 
fwrite($fh, $string); 
fclose($fh); 
$result = mysql_query("LOAD DATA LOCAL INFILE '$myFile'". "INTO TABLE `restartdata` FIELDS TERMINATED BY ',' "); 
    if (!$result) { 
    die("Could not load." . mysql_error()); 
    }else{ 
echo 'data loaded in the database'; 
    } 
+0

我不確定這個問題是什麼? – Epodax

+0

基本上一個文件每天上傳到Linux服務器,我需要每天取它並存儲在數據庫中 – Ben

+1

停止使用**棄用,並且從PHP7中刪除**'mysql_ *'函數。遷移到PDO並開始使用Prepared,Parameterized Queries。 –

回答

2

絕對不使用PHP。當然,沒有什麼是自制的。有一個內置的機制。它被稱爲replication超過15年的嘗試和測試,它被用於成千上萬的安裝。

通過複製可以將來自一個MySQL數據庫服務器(主服務器) 的數據複製到一個或多個MySQL數據庫服務器(從服務器)。 默認情況下,複製是異步的;從站不需要永久連接以接收來自主站的更新。根據 的配置,您可以複製所有數據庫,選擇 數據庫,甚至可以複製數據庫中的選定表。

在PHP中這樣做意味着整個數據庫每天或每小時都會被轉儲,這意味着在轉儲過程中站點沒有響應。然後你必須通過HTTP傳輸整個數據庫。

最後但並非最不重要的是你的PHP方法不允許連續存檔。如果您每天進行一次歸檔,如果系統在最後一次備份23:50小時後失敗會發生什麼情況?

+0

嗯,它太晚了,它是一個Web應用程序,所以它是最好的,如果它是PHP,我已經完成了大部分的技術錯誤,就這樣離開 – Ben

+0

就像我說的那樣,當我輸入這個時,成千上萬的Web應用程序正在被複制。 – e4c5

+0

以及我是一名畢業生,所以沒有經驗,但你可以在php – Ben

相關問題