下面這個文件操作類可以建立目錄,刪除目錄,刪除文件等一系列你能操作操作的功能,代碼如下:
- <?
- /**
- * 操縱文件類
- *
- * 例子:
- * FileUtil::createDir('a/1/2/3'); 測試建立文件夾 建一個a/1/2/3文件夾
- * FileUtil::createFile('b/1/2/3'); 測試建立文件 在b/1/2/文件夾下面建一個3文件
- * FileUtil::createFile('b/1/2/3.exe'); 測試建立文件 在b/1/2/文件夾下面建一個3.exe文件
- * FileUtil::copyDir('b','d/e'); 測試復制文件夾 建立一個d/e文件夾,把b文件夾下的內容復制進去
- * FileUtil::copyFile('b/1/2/3.exe','b/b/3.exe'); 測試復制文件 建立一個b/b文件夾,并把b/1/2文件夾中的3.exe文件復制進去
- * FileUtil::moveDir('a/','b/c'); 測試移動文件夾 建立一個b/c文件夾,并把a文件夾下的內容移動進去,并刪除a文件夾
- * FileUtil::moveFile('b/1/2/3.exe','b/d/3.exe'); 測試移動文件 建立一個b/d文件夾,并把b/1/2中的3.exe移動進去
- * FileUtil::unlinkFile('b/d/3.exe'); 測試刪除文件 刪除b/d/3.exe文件
- * FileUtil::unlinkDir('d'); 測試刪除文件夾 刪除d文件夾
- */
- class FileUtil {
- /**
- * 建立文件夾
- *
- * @param string $aimUrl
- * @return viod
- */
- function createDir($aimUrl) {
- $aimUrl = str_replace('', '/', $aimUrl);
- $aimDir = '';
- $arr = explode('/', $aimUrl);
- foreach ($arr as $str) {
- $aimDir .= $str . '/';
- if (!file_exists($aimDir)) {
- mkdir($aimDir);
- }
- }
- }
- /**
- * 建立文件
- *
- * @param string $aimUrl
- * @param boolean $overWrite 該參數控制是否覆蓋原文件
- * @return boolean
- */
- function createFile($aimUrl, $overWrite = false) {
- if (file_exists($aimUrl) && $overWrite == false) {
- return false;
- } elseif (file_exists($aimUrl) && $overWrite == true) {
- FileUtil::unlinkFile($aimUrl);
- }
- $aimDir = dirname($aimUrl);
- FileUtil::createDir($aimDir);
- touch($aimUrl);
- return true;
- }
- /**
- * 移動文件夾
- *
- * @param string $oldDir
- * @param string $aimDir
- * @param boolean $overWrite 該參數控制是否覆蓋原文件
- * @return boolean
- */
- function moveDir($oldDir, $aimDir, $overWrite = false) {
- $aimDir = str_replace('', '/', $aimDir);
- $aimDir = substr($aimDir, -1) == '/' ? $aimDir : $aimDir . '/';
- $oldDir = str_replace('', '/', $oldDir);
- $oldDir = substr($oldDir, -1) == '/' ? $oldDir : $oldDir . '/';
- if (!is_dir($oldDir)) {
- return false;
- }
- if (!file_exists($aimDir)) {
- FileUtil::createDir($aimDir);
- }
- @$dirHandle = opendir($oldDir);
- if (!$dirHandle) {
- return false;
- }
- while(false !== ($file = readdir($dirHandle))) {
- if ($file == '.' || $file == '..') {
- continue;
- }
- if (!is_dir($oldDir.$file)) {
- FileUtil::moveFile($oldDir . $file, $aimDir . $file, $overWrite);
- } else {
- FileUtil::moveDir($oldDir . $file, $aimDir . $file, $overWrite);
- }
- }
- closedir($dirHandle);
- return rmdir($oldDir);
- }
- /**
- * 移動文件
- *
- * @param string $fileUrl
- * @param string $aimUrl
- * @param boolean $overWrite 該參數控制是否覆蓋原文件
- * @return boolean
- */
- function moveFile($fileUrl, $aimUrl, $overWrite = false) {
- if (!file_exists($fileUrl)) {
- return false;
- }
- if (file_exists($aimUrl) && $overWrite = false) {
- return false;
- } elseif (file_exists($aimUrl) && $overWrite = true) {
- FileUtil::unlinkFile($aimUrl);
- }
- $aimDir = dirname($aimUrl);
- FileUtil::createDir($aimDir);
- rename($fileUrl, $aimUrl);
- return true;
- }
- /**
- * 刪除文件夾
- *
- * @param string $aimDir
- * @return boolean
- */
- function unlinkDir($aimDir) {
- $aimDir = str_replace('', '/', $aimDir);
- $aimDir = substr($aimDir, -1) == '/' ? $aimDir : $aimDir.'/';
- if (!is_dir($aimDir)) {
- return false;
- }
- $dirHandle = opendir($aimDir);
- while(false !== ($file = readdir($dirHandle))) {
- if ($file == '.' || $file == '..') {
- continue;
- }
- if (!is_dir($aimDir.$file)) {
- FileUtil::unlinkFile($aimDir . $file);
- } else {
- FileUtil::unlinkDir($aimDir . $file);
- }
- }
- closedir($dirHandle);
- return rmdir($aimDir);
- }
- /**
- * 刪除文件
- *
- * @param string $aimUrl
- * @return boolean
- */
- function unlinkFile($aimUrl) {
- if (file_exists($aimUrl)) {
- unlink($aimUrl);
- return true;
- } else {
- return false;
- }
- }
- /**
- * 復制文件夾
- *
- * @param string $oldDir
- * @param string $aimDir
- * @param boolean $overWrite 該參數控制是否覆蓋原文件
- * @return boolean
- */
- function copyDir($oldDir, $aimDir, $overWrite = false) {
- $aimDir = str_replace('', '/', $aimDir);
- $aimDir = substr($aimDir, -1) == '/' ? $aimDir : $aimDir.'/';
- $oldDir = str_replace('', '/', $oldDir);
- $oldDir = substr($oldDir, -1) == '/' ? $oldDir : $oldDir.'/';
- if (!is_dir($oldDir)) {
- return false;
- }
- if (!file_exists($aimDir)) {
- FileUtil::createDir($aimDir);
- }
- $dirHandle = opendir($oldDir);
- while(false !== ($file = readdir($dirHandle))) {
- if ($file == '.' || $file == '..') {
- continue;
- }
- if (!is_dir($oldDir . $file)) {
- FileUtil::copyFile($oldDir . $file, $aimDir . $file, $overWrite);
- } else {
- FileUtil::copyDir($oldDir . $file, $aimDir . $file, $overWrite);
- }
- }
- return closedir($dirHandle);
- }
- /**
- * 復制文件
- *
- * @param string $fileUrl
- * @param string $aimUrl
- * @param boolean $overWrite 該參數控制是否覆蓋原文件
- * @return boolean
- */
- function copyFile($fileUrl, $aimUrl, $overWrite = false) {
- if (!file_exists($fileUrl)) {
- return false;
- }
- if (file_exists($aimUrl) && $overWrite == false) {
- return false;
- } elseif (file_exists($aimUrl) && $overWrite == true) {
- FileUtil::unlinkFile($aimUrl);
- }
- $aimDir = dirname($aimUrl);
- FileUtil::createDir($aimDir);
- copy($fileUrl, $aimUrl);
- return true;
- }
- }
- ?>
新聞熱點
疑難解答