Jump to content

Update files / database from remote files / database


clarke78

Recommended Posts

Hi, I'm looking for the simplest, yet most efficient way to update a set of php scripts from a remote website / server. I would like to update certain files / tables + rows in the remote database.

 

I currently have created a very simple updater based on fopen to read my specified updates.php which will tell the scripts updater.php which files to re-write from my own files on my site. 

 

(for the record I had looked into something called Pipes to directly connect to the server or something... it seemed a little complicated though)

 

My Code so far (which works but I'm sure isnt the simplest / best way)

 

Updater.php (on users website)

<?php

$id = $_GET["view"];


$conn = mysql_connect("localhost", "username", "password") or die(mysql_error());
mysql_select_db('table_name', $conn) or die(mysql_error());



if($id == ""){
// make sure the remote file is successfully opened before doing anything else
if ($fp = fopen('http://www.MYURL.net/Updates.txt', 'r')) {
   $content = '';
   // keep reading until there's nothing left 
   while ($line = fgets($fp, 1024)) {
      $update .= $line;
  
  //$content .= $line;
   }} else {
   
   // an error occured when trying to open the specified url 
}
$result = @mysql_query("SELECT * FROM dc_update ORDER BY ID DESC LIMIT 1");
$row = mysql_fetch_array($result);
   print "FILE UPDATED (BETA)<p>Your Version: $row[version]<p>Availible Version: $update<p>";
$compare = "$row[version]";
   
if ("$update" == "$compare"){

print "No updates availible<p>";

} else {

print "Would You like to update to Version $update now?<br>
<form action=$PHP_SELF?view=updateNow method=post>
<input type=hidden value=$update name=update>
<input type=submit value=\"Update\">";

}
}

if($id == "updateNow"){
if ($fp = fopen('http://www.MYURL.net/Update.txt', 'r')) {
   $content = '';
   while ($line = fgets($fp, 1024)) {
      $content .= $line;
  $myFile = "Update.php";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "$content";
fwrite($fh, $stringData);
fclose($fh);

  //$content .= $line;
   }} else {
   
   // an error occured when trying to open the specified url 
}
include_once "Update.php";

$result = @mysql_query("SELECT * FROM dc_update WHERE version=\"$update\"");

while($row = mysql_fetch_array($result)) {
        extract($row);

}

echo "You Have Successfully Installed updates for Version $update.<p>
<a href=Updater.php>Go Back</a>";

}

?>

 

Updates.txt (on my website)

3.0

 

Update.txt (on my website)

<?php

$sql = "INSERT INTO dc_update SET version = \"$update\", files = \"index.php\"";
@mysql_query($sql) or die ("<p>$sql<P>".mysql_errno().": ".mysql_error());

if ($fp = fopen('http://www.MYURL.net/index.php', 'r')) {
   $content = '';
   while ($line = fgets($fp, 1024)) {
      $content .= $line;
  $myFile = "index.php";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "$content";
fwrite($fh, $stringData);
fclose($fh);

  //$content .= $line;
   }} else {
   
   // an error occured when trying to open the specified url 
}

$sql = "INSERT INTO dc_update SET version = \"$update\", files = \"functions.php\"";
@mysql_query($sql) or die ("<p>$sql<P>".mysql_errno().": ".mysql_error());

if ($fp = fopen('http://www.MYURL.net/functions.php', 'r')) {
   $content = '';
   while ($line = fgets($fp, 1024)) {
      $content .= $line;
  $myFile = "functions.php";
$fh = fopen($myFile, 'w') or die("can't open file");
$stringData = "$content";
fwrite($fh, $stringData);
fclose($fh);

  //$content .= $line;
   }} else {
   
   // an error occured when trying to open the specified url 
}

?>

 

Any help / suggestions?

What's wrong with what you've done?

 

This is what I do with shell scripts + a bit of python.

 

I also do a bit of mysql import and export. These are simple text files. I don't cock around with 'a few rows' I dump everything and import it all on another box. The db is like 300meg. I gzip this, ftp it to another server. Then import it.

 

Trying anything more amazing is premature optimization == bad evil  (say if someone changes the db, my backup will still work ;) )

 

Also set all this up as a cron job that runs at 5:00am. Log everything the script is doing as a RSS file, then every morning check your feeds.

 

;D

 

monk.e.boy

Any chance you could go a little more in depth.. i havent set up a cron job before.. and I got a little lost at what you had said you did.

 

Basically I want people to be able to download my set of scripts and be able to check for updates and then update their version from a file on my website.

 

(my scripts use a sql db so I would want those to update through the files somehow.. or have the updater.php be able to take the file from my website and then create / update the db)

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.