-1
我正在致力于公共项目 - 该方法将Git for PHP项目完全替换为FTP部署方法。你放置1个文件 - 在你网站的根目录下部署php。就这样。当你把东西放到到位桶混帐回购协议,该脚本获取ZIP和替换所有网站文件与压缩内容以递归方式删除文件夹时排除文件
我想要做的是,
- 清除(在我的根目录)的文件夹中的脚本位于不包括此脚本的.htaccess
- 然后下载的zip
- 解压缩至$ DEST
- 复制$ dest中的内容复制到根
- 删除蒸馏水和所有它的内容
换句话说,它必须用新的zip内容替换整个根目录,不包括脚本本身和其他几个文件(列在$ exc数组中)。就这样。问题是,我的功能rmdir_recursively
不排除文件,删除所有包括脚本。我错过了什么?
您可以为脚本提供哪些其他优化建议?
Thx提前。
<?php
// Set these dependant on your BB credentials
$username = '';
$password = '';
// your Bitbucket repo name
$reponame = "";
// extract to
$dest = "./"; // leave ./ for relative destination
//Exclusion list
$exc = array("deploy.php", ".htaccess");
// Grab the data from BB's POST service and decode
$json = stripslashes($_POST['payload']);
$data = json_decode($json);
// set higher script timeout (for large repo's or slow servers)
set_time_limit(5000);
// Set some parameters to fetch the correct files
$uri = $data->repository->absolute_url;
$node = $data->commits[0]->node;
$files = $data->commits[0]->files;
//Clear Root
rmdir_recursively(".");
// download the repo zip file
$fp = fopen("tip.zip", 'w');
$ch = curl_init("https://bitbucket.org/$username/$reponame/get/$node.zip");
curl_setopt($ch, CURLOPT_USERPWD, "$username:$password");
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, true);
curl_setopt($ch, CURLOPT_FILE, $fp);
$data = curl_exec($ch);
curl_close($ch);
fclose($fp);
// unzip
$zip = new ZipArchive;
$res = $zip->open('tip.zip');
if ($res === TRUE) {
$zip->extractTo('./');
$zip->close();
} else {
die('ZIP not supported on this server!');
}
// function to delete all files in a directory recursively
function rmdir_recursively($dir) {
global $exc;
if(in_array($dir,$exc)) return false;
if (!is_dir($dir) || is_link($dir)) return unlink($dir);
foreach (scandir($dir) as $file) {
if ($file == '.' || $file == '..') continue;
if (!rmdir_recursively($dir . DIRECTORY_SEPARATOR . $file)) {
chmod($dir . DIRECTORY_SEPARATOR . $file, 0777);
if (!rmdir_recursively($dir . DIRECTORY_SEPARATOR . $file)) return false;
};
}
return rmdir($dir);
}
// function to recursively copy the files
function copy_recursively($src, $dest) {
if (is_dir($src)) {
if ($dest != "./")
rmdir_recursively($dest);
@mkdir($dest);
$files = scandir($src);
foreach ($files as $file)
if ($file != "." && $file != "..")
copy_recursively("$src/$file", "$dest/$file");
}
else if (file_exists($src))
copy($src, $dest);
rmdir_recursively($src);
}
// start copying the files from extracted repo and delete the old directory recursively
copy_recursively("$username-$reponame-$node", $dest);
// delete the repo zip file
unlink("tip.zip");
?>
*“你可以建议脚本其他什么优化?” * - 用'DirectoryIterator'使用文件系统抽象,它是递归的吊坠。提供一个'FilterIterator'来排除你的文件。 – hakre
@hakre你可以请你的代码建议吗? – heron
@hakre这是公共项目,我试图让git和php爱好者的生活变得轻松,这就是为什么你不仅仅帮助我而是社区 – heron