0

Hi I need to create something automatic that connects to another server using a php library and then I need to load the data in a mysql database only the first file uploads a new file is uploaded everyday. The problem is how do I continue uploading a file everyday to the database I am almost there

Here is the code:

<?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'; } 
3
  • I'm not sure what the question is? Commented Jul 29, 2016 at 10:03
  • Basically a file uploads everyday to the linux server and I need to fetch it everyday and store in a database Commented Jul 29, 2016 at 10:05
  • 1
    Stop using the deprecated and as of PHP7 removed mysql_* functions. Migrate to PDO and start using Prepared, Parameterized Queries. Commented Jul 29, 2016 at 10:20

1 Answer 1

2

Definitely not using PHP. And definitely nothing that's home made. There is a built in mechanism for that. It's called replication tried and tested over more than 15 years and it's being used on thousands of installations.

Replication enables data from one MySQL database server (the master) to be copied to one or more MySQL database servers (the slaves). Replication is asynchronous by default; slaves do not need to be connected permanently to receive updates from the master. Depending on the configuration, you can replicate all databases, selected databases, or even selected tables within a database.

To do this in PHP would mean the entire database is dumped daily or hourly during which means the site is unresponsive during the time the dump is being made. Then you have to transfer the whole database over HTTP.

Last but not least your PHP approach does not allow continous archiving. If you are archiving once a day, what happens if the system fails 23:50 hours after the last backup was made?

Sign up to request clarification or add additional context in comments.

7 Comments

well it is too late it is a web application so it is best if it is php and I have done most of the technical errors just this left
Like I said thousands of web applications are being replicated as I type this.
well I am a graduate so not that experienced but you can do it in php
See my updated answer.
Good luck, I hope you will mark the answer correct in one years time when you eventually make the switch to replication.
|

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.