在这里回答我自己的问题是为了保持问题的答案。这样做的目的是在升级之前复制重要目录,并在升级之后恢复它:
function my_dir_copy($source, $dest)
{
if (is_link($source)) {
return symlink(readlink($source), $dest);
}
if (is_file($source)) {
return copy($source, $dest);
}
if (!is_dir($dest)) {
mkdir($dest);
}
$dir = dir($source);
while (false !== $entry = $dir->read()) {
if ($entry == \'.\' || $entry == \'..\') {
continue;
}
my_dir_copy("$source/$entry", "$dest/$entry");
}
$dir->close();
return true;
}
function my_dir_rmdirr($dirname)
{
if (!file_exists($dirname)) {
return false;
}
if (is_file($dirname)) {
return unlink($dirname);
}
$dir = dir($dirname);
while (false !== $entry = $dir->read()) {
if ($entry == ‘.’ || $entry == ‘..’) {
continue;
}
rmdirr(“$dirname/$entry”);
}
$dir->close();
return rmdir($dirname);
}
function my_dir_backup()
{
$to = dirname(__FILE__)."/../igr_backup/";
$from = dirname(__FILE__)."/whatever_directory_you_preserve/";
my_dir_copy($from, $to);
}
function my_dir_recover()
{
$from = dirname(__FILE__)."/../igr_backup/";
$to = dirname(__FILE__)."/whatever_directory_you_preserve/";
my_dir_copy($from, $to);
if (is_dir($from)) {
my_dir_rmdirr($from);
}
}
add_filter(\'upgrader_pre_install\', \'my_dir_backup\', 10, 2);
add_filter(\'upgrader_post_install\', \'my_dir_recover\', 10, 2);