私は公開プロジェクトに取り組んでいます。これは、PHP ベースのプロジェクトの FTP 展開方法を GIT に完全に置き換えるものです。1 つのファイルを配置します - Web サイトのルート ディレクトリに php をデプロイします。それで全部です。Bitbucket git リポジトリに何かをプッシュすると、このスクリプトは zip を取得し、すべての Web サイト ファイルを zip コンテンツに置き換えます
私がやりたいことは、
- このスクリプトと.htaccessを除外して、スクリプトが配置されているフォルダー(私の場合はルート)をクリアします
- 次に、zipをダウンロードします
- $dest に解凍する
- $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");
?>