將常用檔案系統操作 (讀寫、複製、移動、刪除、權限、掃描目錄等) 封裝為可複用類別,能大幅減少重複代碼並統一錯誤處理。本文呈現一個名為 FSC (File System Class) 的經典實作架構。
<?php
/***********************************************************************************/
/* Program: File System Class (FSC) */
/* Author: 吳雙 (James Wu) */
/* Email: wu-shuang@163.net */
/* HomePage: https://www.dev-php.com */
/* Date: 2002-01-24 */
/***********************************************************************************/
class FSC {
/**************************************************************************/
// 方法: getfilesource
// 功能: 取得指定檔案完整內容
// 參數: $file 目標檔案路徑
// 回傳: 字串 (檔案內容) 或 false (失敗)
/**************************************************************************/
function getfilesource($file) {
if (!file_exists($file) || !is_readable($file)) return false;
$fp = fopen($file, 'rb');
$content = fread($fp, filesize($file));
fclose($fp);
return $content;
}
/**************************************************************************/
// 方法: writefile
// 功能: 將資料寫入檔案 (自動建立目錄)
// 參數: $file 路徑, $data 內容, $mode 權限 (預設 0644)
// 回傳: bool
/**************************************************************************/
function writefile($file, $data, $mode = 0644) {
$dir = dirname($file);
if (!is_dir($dir)) $this->mkdirtree($dir);
$fp = @fopen($file, 'wb');
if (!$fp) return false;
$result = fwrite($fp, $data);
fclose($fp);
if ($result !== false) @chmod($file, $mode);
return $result !== false;
}
/**************************************************************************/
// 方法: mkdirtree
// 功能: 遞迴建立多層目錄
// 參數: $path 路徑, $mode 權限 (預設 0755)
// 回傳: bool
/**************************************************************************/
function mkdirtree($path, $mode = 0755) {
if (is_dir($path)) return true;
$parent = dirname($path);
if ($parent != '.' && $parent != '/') {
$this->mkdirtree($parent, $mode);
}
return @mkdir($path, $mode);
}
/**************************************************************************/
// 方法: copyfile
// 功能: 複製單一檔案
// 參數: $src 來源, $dst 目的
// 回傳: bool
/**************************************************************************/
function copyfile($src, $dst) {
if (!file_exists($src) || !is_readable($src)) return false;
$dstDir = dirname($dst);
if (!is_dir($dstDir)) $this->mkdirtree($dstDir);
return @copy($src, $dst);
}
/**************************************************************************/
// 方法: movefile
// 功能: 移動/重命名檔案 (跨分區自動複製+刪除)
// 參數: $src 來源, $dst 目的
// 回傳: bool
/**************************************************************************/
function movefile($src, $dst) {
if (!file_exists($src)) return false;
$dstDir = dirname($dst);
if (!is_dir($dstDir)) $this->mkdirtree($dstDir);
if (@rename($src, $dst)) return true; // 同分區快速
// 跨分區 fallback
if ($this->copyfile($src, $dst)) {
return @unlink($src);
}
return false;
}
/**************************************************************************/
// 方法: deletefile
// 功能: 刪除檔案 (不遞迴目錄)
// 參數: $file 路徑
// 回傳: bool
/**************************************************************************/
function deletefile($file) {
return is_file($file) && is_writable($file) ? @unlink($file) : false;
}
/**************************************************************************/
// 方法: deletetree
// 功能: 遞迴刪除目錄及所有內容
// 參數: $dir 目錄路徑
// 回傳: bool
/**************************************************************************/
function deletetree($dir) {
if (!is_dir($dir)) return false;
$items = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator($dir, RecursiveDirectoryIterator::SKIP_DOTS),
RecursiveIteratorIterator::CHILD_FIRST
);
foreach ($items as $item) {
if ($item->isDir()) @rmdir($item->getPathname());
else @unlink($item->getPathname());
}
return @rmdir($dir);
}
/**************************************************************************/
// 方法: getdirlist
// 功能: 取得目錄列表 (含檔案資訊陣列)
// 參數: $path 目錄, $pattern 可選 glob 模式
// 回傳: 陣列 [['name'=>, 'path'=>, 'size'=>, 'mtime'=>, 'is_dir'=>], ...]
/**************************************************************************/
function getdirlist($path, $pattern = '*') {
if (!is_dir($path)) return array();
$list = array();
$files = glob($path . DIRECTORY_SEPARATOR . $pattern, GLOB_MARK);
foreach ($files as $file) {
$stat = stat($file);
$list[] = array(
'name' => basename($file),
'path' => $file,
'size' => $stat['size'],
'mtime' => $stat['mtime'],
'is_dir' => is_dir($file)
);
}
return $list;
}
}
?>
<?php
require_once 'FSC.php';
$fsc = new FSC();
// 讀取檔案
$content = $fsc->getfilesource('/var/www/config.ini');
// 寫入檔案 (自動建立目錄)
$fsc->writefile('/var/www/data/cache/settings.json', json_encode(['debug'=>true]));
// 遞迴複製目錄 (需自行實作 copytree,參考 movefile 邏輯)
// 刪除整棵目錄
$fsc->deletetree('/var/www/data/temp');
// 掃描目錄
$files = $fsc->getdirlist('/var/www/uploads', '*.{jpg,png,gif}');
foreach ($files as $f) {
echo $f['name'] . ' (' . $f['size'] . ' bytes)' . PHP_EOL;
}
?>
SplFileObject、FilesystemIterator、RecursiveIteratorIterator 等 SPL 類別,效能更好且記憶體友善。declare(strict_types=1)。RuntimeException 或自訂例外,避免靜默失敗。League\Flysystem 抽象層,支援本地、FTP、S3、Azure Blob、Google Cloud Storage 統一 API。