PHP 5 引入 ActiveScript 引擎,允許 PHP 作為 Windows Script Host (WSH) 或 Internet Explorer 的客戶端腳本語言運行。不再局限於伺服器端!
C:\Program Files\EasyPHP5\phpcd C:\Program Files\EasyPHP5\php
regsvr32 php5activescript.dll
彈出「DllRegisterServer in php5activescript.dll succeeded」即成功。
建立 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 的瀏覽器),畫面顯示字串。
<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,按時間命名存入備份目錄。
system('command'); // 同步執行,回傳最後一行輸出
exec('command', $output, $return_var); // 取得完整輸出陣列
shell_exec('command'); // 回傳完整輸出字串
passthru('command'); // 直接輸出二進位資料
# 基本語法
rar a -r -ep1 backup.rar @filelist.txt
# 參數說明
# a : 加入壓縮
# -r : 遞迴子目錄
# -ep1 : 排除基礎路徑,只存相對路徑
# @filelist.txt : 從檔案讀取要壓縮的檔案清單
mysqldump -u user -p'pass' --single-transaction --routines --triggers dbname > backup.sql
<?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}";
?>
php.exe,參數 C:\path\backup.php| 元件 | 版本/要求 |
|---|---|
| PHP | 5.0.0+ (需 php5activescript.dll) |
| OS | Windows (Win2000/XP/2003/Vista/7/10/11) |
| Browse | IE / HTA / WSH (cscript/wscript) |
| RAR | WinRAR 命令列版 (rar.exe) 或 UnRAR |
| MySQL | mysqldump 在 PATH 中 |
ActivePHP 已淘汰 (IE 退場、ActiveScript 棄用)。現代同類需求建議: