# git_repo_web_sync Simple php script that after being requested syncs git repo with a catalog on the server ```php Access Denied"; echo "
Invalid or missing synchronization token.
"; exit; } echo "Target directory exists. Cleaning contents (including hidden files)...
"; // HOW: // 1. cd into the directory // 2. rm -rf * .* (excluding . and .. which is x_x) // I use a subshell (cd ... && ...) to ensure we only delete inside targetDir // Note: I must be very careful with '.*'. I excluded '.' and '..' manually. // Command: find . -mindepth 1 -delete is safer and handles hidden files WHICH IS IMPORTANT. $cleanCmd = "cd " . escapeshellarg($targetDir) . " && find . -mindepth 1 -delete"; exec($cleanCmd, $cleanOut, $cleanStatus); if ($cleanStatus !== 0) { echo "Error: Failed to clean directory contents.
"; echo "Path: " . htmlspecialchars($targetDir) . "
"; echo "" . htmlspecialchars(implode("\n", $cleanOut)) . "";
exit;
}
echo "Directory contents cleaned successfully.
"; } else { echo "Target directory did not exist. Creating...
"; // Create the directory if it doesn't exist SHOULDN'T be needed but addedd just in case if (!mkdir($targetDir, 0755, true)) { echo "Error: Failed to create directory.
"; exit; } } //safety check $parentDir = dirname($targetDir); if (!is_dir($parentDir)) { echo "Error: Parent directory ($parentDir) does not exist.
"; exit; } /* //dummy if (!is_writable($parentDir)) { echo "Error: Web server cannot write to parent directory ($parentDir).
"; echo "Run: sudo chown -R www-data:www-data $parentDir
Cloning $repoUrl (branch: $branch) into $targetDir...
SUCCESS: Repository synchronized successfully!
"; echo "Files are now located at: $targetDir
FAILED: Git clone failed.
"; echo "";
echo htmlspecialchars(implode("\n", $output));
echo "";
echo "Synchronization finished.
"; ?> ```