
Are you tired of copying and restoring your files every time you make a mistake in the source code and save? Check repPHP!
Requirements
- Host with last PHP version support. (if you want to run it in localhost, check WampServer)
- The latest repPHP script.
How it works?
It’s very simple system of two files:
- backup.php
- repair.php
Now let’s see what these files do.
The backup.php file
Let’s take a look in the source of this file:
echo 'repPHP 0.2b - the backup and repair script<br/>http://repphp.sourceforge.net<br/>';
echo 'Begin the backup!<br/>';
$files = glob("*.*");
echo '<br/>Checking files...<br/>';
foreach($files as $filename)
{
if($filename != 'repair.php' AND $filename != 'backup.php')
{
$dir = 'backup';
$copyfilename = $dir.'/'.$filename;
if(!is_dir($dir))
{
mkdir($dir);
}
echo '<br/>Copying '.$filename;
$c = copy($filename, $copyfilename);
if(!$c)
{
echo '<br/>Error copying '.$filename;
}else{
echo '<br/>'.$filename.' copied with success!';
}
}
}
echo '<br/><br/>Done!';
Now let’s see what this does:
- Read all files in the directory of the script.
- For each file found, the script will copy to a backup directory ($dir).
That’s it! (for backup.php file) :)
The repair.php file
The source:
if (isset($_GET['c']))
{
echo 'repPHP 0.2b - the backup and repair script<br/>http://repphp.sourceforge.net<br/>';
echo 'Begin the repair!<br/>';
$files = glob("*.*");
echo '<br/>Checking files...<br/>';
foreach($files as $filename)
{
if($filename != 'repair.php' AND $filename != 'backup.php')
{
$dir = 'backup';
$checkfilename = $dir.'/'.$filename;
$open = fopen($filename,'r');
$open2 = fopen($checkfilename,'r');
if(!$open or !$open2)
{
echo '<br/>File '.$checkfilename.' not found.<br/>';
}else{
$content = fread($open, filesize($filename));
$content2 = fread($open2, filesize($checkfilename));
if($content == $content2)
{
echo '<br/>File '.$filename.' is OK.';
}else{
echo '<br/>File '.$filename.' is NOT OK. Repairing...';
$c = copy($checkfilename, $filename);
if(!$c)
{
echo '<br/>Error repairing '.$filename;
}else{
echo '<br/>'.$filename.' repaired!';
}
}
}
}
}
echo '<br/><br/>Done!';
}
else
{
echo 'repPHP 0.2b - the backup and repair script<br/>
http://repphp.sourceforge.net<br/>';
echo '<p>Are you sure about repairing the files? (this operation will repair ALL modified files)</p>' . "\n";
echo '<p><a href="' . $_SERVER['PHP_SELF'] . '?c=yes">Yes</a> - <a href="' . ((isset($_SERVER['HTTP_REFERER'])) ? $_SERVER['HTTP_REFERER'] : 'http://blog.estevaomascarenhas.com/') . '">No</a>';
}
What this does:
- Ask if you are sure about repair the files.
- Read all files in the directory of the script.
- For each file found, the script will verify the file content with content of the same-named file in the backup directory($dir).
- If the contents are different, the script will restore the file using the backup file.
Now, let’s see how you can use this.
- Put the files backup.php and repair.php in the directory that you want to make a backup.
- Run the file backup.php in your browser.
- Now you can modify your files with security, if something get wrong, just run the file repair.php in your browser.
BEWARE! Pay attention, if you run repair.php and click in the Yes link, your files will be replaced by the backups files with no confirmation!
You can download these files here.
Please, recommend this to your friends and for any doubts, suggestions and critics, leave a comment here or send me an e-mail.
Simple and useful; I never thinked a script like this! Keep up the good work! :)
Thanks GHS! ;)
There is much more easier create one file “backup.php” and he even copy my files and see what is newer and overwrite the old?
Ah, Why this blog are in english?
I thought about it and I have decided to create two different files, because the user may only want to make the backup and not repair the files in the same time. :D
Because english is the universal language and most of people speak english. ;)