PHP常用技術文之檔案操作和目錄操作總結,_PHP教程

來源:互聯網
上載者:User

PHP常用技術文之檔案操作和目錄操作總結,


一、基本檔案的操作

檔案的基本操作有:檔案判斷、目錄判斷、檔案大小、讀寫性判斷、存在性判斷及檔案時間等

<?php   header("content-type","text/html;charset=utf-8"); /* *聲明一個函數,傳入檔案名稱擷取檔案屬性 *@param string $fileName 檔案名稱 */ function getFilePro($fileName) {   if(!file_exists($fileName))   {     echo '檔案不存在
'; return; } /*是否是普通檔案*/ if(is_file($fileName)) { echo $fileName.'是一個檔案
'; } /*是否是目錄*/ if(is_dir($fileName)) { echo $fileName.'是一個目錄'; } /*輸出檔案的型態*/ echo '檔案型態是:'.getFileType($fileName).'
'; /*檔案大小,轉換單位*/ echo '檔案大小是:'.getFileSize(filesize($fileName)).'
'; /*檔案是否可讀*/ if(is_readable($fileName)) { echo '檔案可讀
'; } /*檔案是否可寫*/ if(is_writable($fileName)) { echo '檔案可寫
'; } /*檔案是否可執行*/ if(is_executable($fileName)) { echo '檔案可執行
'; } echo '檔案創立時間:'.date('Y年m月j日',filectime($fileName)).'
'; echo '檔案最後修改時間:'.date('Y年m月j日',filemtime($fileName)).'
'; echo '檔案最後開啟時間:'.date('Y年m月j日',fileatime($fileName)).'
'; } /* *聲明一個函數,返迴文件類型 *@param string $fileName 檔案名稱 */ function getFileType($fileName) { $type = ''; switch(filetype($fileName)) { case 'file':$type .= '普通檔案';break; case 'dir':$type .= '目錄檔案';break; case 'block':$type .= '塊裝置檔案';break; case 'char':$type .= '字元裝置檔案';break; case 'filo':$type .= '管道檔案';break; case 'link':$type .= '符號連結';break; case 'unknown':$type .= '未知檔案';break; default: } return $type; } /* *聲明一個函數,返迴文件大小 *@param int $bytes 檔案位元組數 */ function getFileSize($bytes) { if($bytes >= pow(2,40)) { $return = round($bytes / pow(1024,4),2); $suffix = 'TB'; } else if($bytes >= pow(2,30)) { $return = round($bytes / pow(1024,3),2); $suffix = 'GB'; } else if($bytes >= pow(2,20)) { $return = round($bytes / pow(1024,2),2); $suffix = 'MB'; } else if($bytes >= pow(2,10)) { $return = round($bytes / pow(1024,1),2); $suffix = 'KB'; } else { $return = $bytes; $suffix = 'B'; } return $return." ".$suffix;} /*調用函數,傳入test目錄下的test.txt檔案*/ getFilePro('./test/div.html');?>

結果:

二、目錄的操作

目錄的操作有:遍曆目錄、刪除、複製、大小統計等

1、遍曆目錄

 /* *遍曆目錄 *@param string $dirName 目錄名 */ function findDir($dirName) {   $num = 0; /*統計子檔案個數*/   $dir_handle = opendir($dirName); /*開啟目錄*/   /*輸出目錄檔案*/   echo '
 
  ';   echo '
  ';   echo '
  
    檔案名稱
    '; while($file = readdir($dir_handle)) { $dirFile = $dirName.'/'.$file; $bgcolor = $num++%2==0?'#ffffff':'#cccccc'; echo '
   
    '; echo '
    '; echo '
    '; echo '
    '; echo '
    '; echo '
   '; } echo "
  

目錄'.$dirName.'下的檔案

檔案大小 檔案類型 修改時間
'.$file.''.filesize($dirFile).''.filetype($dirFile).''.date('Y/n/t',filemtime($dirFile)).'
"; closedir($dir_handle); /*關閉目錄*/ echo "在".$dirName."目錄下共有".$num.'個子檔案'; } /*傳遞目前的目錄下的test目錄*/ findDir('./test');

結果

2、統計目錄大小

 /* *統計目錄大小 *@param string $dirName 目錄名 *@return double */function dirSize($dirName){   $dir_size = 0;   if($dir_handle = @opendir($dirName))   {     while ($fileName = readdir($dir_handle))     {       /*排除兩個特殊目錄*/       if($fileName != '.' && $fileName != '..')       {         $subFile = $dirName.'/'.$fileName;         if(is_file($subFile))         {           $dir_size += filesize($subFile);         }         if(is_dir($subFile))         {           $dir_size += dirSize($subFile);         }       }     }     closedir($dir_handle);     return $dir_size;   } } /*傳遞目前的目錄下的test目錄*/ $dir_size = dirSize('./test'); echo './test目錄檔案大小是:'.round($dir_size / pow(1024,1),2).'KB';

結果:

3、刪除目錄

/**刪除目錄*@param string $dirName 目錄名 */ function delDir($dirName) {   /*php中的mkdir函數就可以建立目錄*/   if(file_exists($dirName))   {     if($dir_handle = @opendir($dirName))     {       while ($fileName = readdir($dir_handle))       {         /*排除兩個特殊目錄*/         if($fileName != '.' && $fileName != '..')         {           $subFile = $dirName.'/'.$fileName;           if(is_file($subFile))           {             unlink($subFile);           }           if(is_dir($subFile))           {             delDir($subFile);           }         }       }       closedir($dir_handle);       rmdir($dirName);       return $dirName.'目錄已經刪除';     }   } } /*傳遞test目錄的副本test1*/ echo delDir('./test1'); 

刪除成功的提示資訊

4、複製目錄

 /* *複製目錄 *@param string $dirSrc 原目錄名 *@param string $dirTo 目標目錄名 */ function copyDir($dirSrc,$dirTo) {   if(is_file($dirTo))   {     echo '目標目錄不能建立';/*目標不是一個*/     return;   }   if(!file_exists($dirTo))   {     /*目錄不存在則建立*/     mkdir($dirTo);   }   if($dir_handle = @opendir($dirSrc))   {     while ($fileName = readdir($dir_handle))     {       /*排除兩個特殊目錄*/       if($fileName != '.' && $fileName != '..')       {         $subSrcFile = $dirSrc.'/'.$fileName;         $subToFile = $dirTo.'/'.$fileName;         if(is_file($subSrcFile))         {           copy($subSrcFile,$subToFile);         }         if(is_dir($subSrcFile))         {           copyDir($subSrcFile,$subToFile);         }       }     }     closedir($dir_handle);     return $dirSrc.'目錄已經複製到'.$dirTo.'目錄';   } } echo copyDir('./test','../testcopy');



http://www.bkjia.com/PHPjc/885647.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/885647.htmlTechArticlePHP常用技術文之檔案操作和目錄操作總結, 一、基本檔案的操作 檔案的基本操作有:檔案判斷、目錄判斷、檔案大小、讀寫性判斷、存在...

  • 聯繫我們

    該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

    如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.