【发布时间】:2014-10-19 10:44:44
【问题描述】:
我创建了自己的备份系统,用于备份我的数据库和网站上可能发生更改的各种文件夹(为简单起见,假设一个文件夹 -> folder_to_backup 以及我想要保留的文件和文件夹)。唯一的问题是,它不适用于本地环境(localhost),这是我想解决的问题。这是执行实际压缩的类(忘记我从哪里得到它):
function __construct($file, $folders = array(), $ignored = NULL)
{
$this->zip = new ZipArchive();
$this->ignored_names = is_array($ignored) ? $ignored : $ignored ? array(
$ignored
) : array();
if ($this->zip->open($file, ZIPARCHIVE::CREATE) !== TRUE) {
return FALSE;
}
$folder = substr($folder, -1) == '/' ? substr($folder, 0, strlen($folder) - 1) : $folder;
if (strstr($folder, '/')) {
$this->root = substr($folder, 0, strrpos($folder, '/') + 1);
$folder = substr($folder, strrpos($folder, '/') + 1);
}
foreach ($folders as $folder) {
$this->zip($folder);
}
$this->zip->close();
}
function zip($folder, $parent = NULL)
{
$full_path = $this->root . $parent . $folder;
$zip_path = $parent . $folder;
$this->zip->addEmptyDir($zip_path);
$dir = new DirectoryIterator($full_path);
foreach ($dir as $file) {
if (!$file->isDot()) {
$filename = $file->getFilename();
if (!in_array($filename, $this->ignored_names)) {
if ($file->isDir()) {
$this->zip($filename, $zip_path . '/');
} else {
$this->zip->addFile($full_path . '/' . $filename, $zip_path . '/' . $filename);
}
}
}
}
}
因此,我使用上面的代码创建了我的 zip 文件,但是我必须发送 $folder 和 realpath() 以使迭代器工作,因此在本地环境中我会得到这样的结果:
C:\xampp\htdocs\sitename\cms\files\folder_to_backup\
在 http 环境中:
/opt/www/prezent/sitename/HTML/cms/files/folder_to_backup/
假设我想更新我的站点的本地主机副本,我下载了文件,但由于目录结构不兼容,我无法解压缩它,目录分隔符也不兼容。
所以我想我可以隔离'cms' . DIRECTORY_SEPARATOR . 'files' . DIRECTORY_SEPARATOR . 'folder_to_backup 的共同点。所以本质上不是让所有文件夹通向文件夹 cms 我只会有结构
DIR
files /
folder_to_backup /
some files and folders ... etc
然后在还原时,而不是通过realpath(DIRECTORY_SEPARATOR) 将其提取到/opt/ 或C:\。我会使用类似于realpath(dirname(__FILE__)) 的东西或者你有什么。
简而言之,我的问题是如何获得上述文件结构(不包括直到/files/ 的真实路径,知道我必须为 zip 创建者提供真实路径以便它可以找到文件?
【问题讨论】:
标签: php iterator directory zip