萬盛學電腦網

您现在的位置: 萬盛學電腦網 >> 程式設計 >  >> 網路程式設計 >> PHP 程式設計

ActivePHP 實戰:在 Windows 上用 PHP 寫客戶端腳本打造版本管理系統

什麼是 ActivePHP?

PHP 5 引入 ActiveScript 引擎,允許 PHP 作為 Windows Script Host (WSH) 或 Internet Explorer 的客戶端腳本語言運行。不再局限於伺服器端!

安裝啟用步驟

  1. 下載 PHP 5 Windows 套件 (zip 非 installer)
  2. 解壓至目錄,例:C:\Program Files\EasyPHP5\php
  3. 以管理員身分開啟命令提示字元:
cd C:\Program Files\EasyPHP5\php
regsvr32 php5activescript.dll

彈出「DllRegisterServer in php5activescript.dll succeeded」即成功。

Hello World 測試

建立 hello.htm:

<!DOCTYPE html>
<html><head><title>ActivePHP Test</title></head><body>
<script language="ActivePHP5">
    $document->write('Hello World from PHP!');
</script>
</body></html>

雙擊開啟 (需 IE 或支援 ActiveScript 的瀏覽器),畫面顯示字串。

進階:呼叫 Windows 原生 API

<script language="ActivePHP5">
    $window->alert('Hello from PHP via Windows MessageBox!');
    
    // 建立 COM 物件
    $fso = new COM("Scripting.FileSystemObject");
    $file = $fso->CreateTextFile("C:\\test_php.txt", true);
    $file->WriteLine("Written by ActivePHP at " . date("Y-m-d H:i:s"));
    $file->Close();
    
    $shell = new COM("WScript.Shell");
    $shell->Run("notepad.exe C:\\test_php.txt");
</script>

版本管理系統實作需求

將開發目錄下的檔案 + 資料庫備份打包成 RAR,按時間命名存入備份目錄。

核心技術點

1. PHP 執行外部程式

system('command');  // 同步執行,回傳最後一行輸出
exec('command', $output, $return_var);  // 取得完整輸出陣列
shell_exec('command');  // 回傳完整輸出字串
passthru('command');    // 直接輸出二進位資料

2. RAR 命令列參數

# 基本語法
rar a -r -ep1 backup.rar @filelist.txt

# 參數說明
# a          : 加入壓縮
# -r         : 遞迴子目錄
# -ep1       : 排除基礎路徑,只存相對路徑
# @filelist.txt : 從檔案讀取要壓縮的檔案清單

3. MySQL 資料庫備份

mysqldump -u user -p'pass' --single-transaction --routines --triggers dbname > backup.sql

4. 完整備份腳本 (backup.php)

<?php
// 設定
$sourceDir = 'C:/www/project';
$backupDir = 'D:/backups';
$dbHost = 'localhost';
$dbUser = 'root';
$dbPass = 'secret';
$dbName = 'myapp';
$rarPath = '"C:/Program Files/WinRAR/Rar.exe"';

// 時間戳
$timestamp = date('Ymd_His');
$backupName = "backup_{$timestamp}";
$tempDir = sys_get_temp_dir() . "/{$backupName}";
mkdir($tempDir, 0777, true);

// 1. 複製專案檔案 (排除 .git, vendor, logs, cache)
$exclude = ['.git', 'vendor', 'logs', 'cache', 'tmp', '.idea'];
$files = new RecursiveIteratorIterator(
    new RecursiveDirectoryIterator($sourceDir, RecursiveDirectoryIterator::SKIP_DOTS)
);
$fileList = [];
foreach ($files as $file) {
    $rel = $file->isDir() ? '' : $file->getPathname();
    $skip = false;
    foreach ($exclude as $ex) {
        if (strpos($file->getPathname(), $ex) !== false) { $skip = true; break; }
    }
    if (!$skip && $file->isFile()) $fileList[] = $rel;
}
file_put_contents("{$tempDir}/filelist.txt", implode("\n", $fileList));

// 2. 匯出資料庫
$dbDump = "{$tempDir}/db.sql";
exec("mysqldump -u{$dbUser} -p{$dbPass} -h{$dbHost} --single-transaction {$dbName} > {$dbDump}", $out, $ret);
if ($ret !== 0) die("mysqldump failed");

// 3. 壓縮
$rarFile = "{$backupDir}/{$backupName}.rar";
$cmd = "{$rarPath} a -r -ep1 {$rarFile} @{$tempDir}/filelist.txt {$dbDump}";
exec($cmd, $out, $ret);
if ($ret !== 0) die("RAR failed: " . implode("\n", $out));

// 4. 清理暫存
array_map('unlink', glob("{$tempDir}/*"));
rmdir($tempDir);

echo "備份完成: {$rarFile}";
?>

排程自動執行

  • Windows 工作排程器:觸發程式 php.exe,參數 C:\path\backup.php
  • 每日/每週/事件觸發

環境需求總結

元件版本/要求
PHP5.0.0+ (需 php5activescript.dll)
OSWindows (Win2000/XP/2003/Vista/7/10/11)
BrowseIE / HTA / WSH (cscript/wscript)
RARWinRAR 命令列版 (rar.exe) 或 UnRAR
MySQLmysqldump 在 PATH 中

現代替代方案

ActivePHP 已淘汰 (IE 退場、ActiveScript 棄用)。現代同類需求建議:

  • Electron / Tauri:JS/TS + Node.js 打包桌面應用
  • Python + PyInstaller:腳本打包單檔執行檔
  • Go / Rust:原生編譯、單一二進位、跨平台
  • PHP CLI + Cron/Task Scheduler:伺服器端定時任務 (最推薦)
copyright © 萬盛學電腦網 all rights reserved