PHP伺服器檔案管理工具開發小結(五):擷取檔案屬性資訊

來源:互聯網
上載者:User

標籤:php   lamp   檔案管理工具   

回顧第三節列出的基本代碼架構,作者為枚舉檔案屬性資訊預留了不少的表頭:

echo "<table>";echo "<tr><th>Name</th><th>Type</th><th>Readable</th><th>Writable</th><th>Excutable</th><th>Size</th><th>Created</th><th>Modified</th><th>Access</th><th>Operation</th></tr>";

如:

650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/59/C6/wKioL1ThlziwwGNSAAEcAQf4X-M397.jpg" title="PHP檔案管理工具4.png" alt="wKioL1ThlziwwGNSAAEcAQf4X-M397.jpg" />

從左至右分別為名稱、類型、可讀性、可寫性、可執行性、大小、建立時間、修改時間、訪問時間、操作。類型為檔案夾、檔案、串連等,可讀、可寫、可執行通過對號和錯號表徵圖標識True or False。大小提供一個約數,建立、修改、訪問時間提供標準時間表示,操作欄則列出對該元素可執行檔操作。

具體到檔案,每一行的內容通過getFileElemInfo函數列出:

        if (isset($fileList))        {            $isDirEmpty = false;            natsort($fileList);            foreach($fileList as $fileName)            {                echo getFileElemInfo($strDirName, $fileName);            }        }

使用natsort,可以為檔案名稱提供比較合理的排序模式。如果需要更複雜的排序,可以使用usort,這裡不做展開。


通過stat()函數可以得到檔案比較多的屬性了,而檔案類型可以通過filetype得到。

下面是前兩項的描述方法:

function getDirElemInfo($dirPath, $elemName){    $dirPath = rtrim($dirPath, "/");    $filePath = $dirPath."/".$elemName;    if (!($res = stat($filePath)))        return "";    $info = "<tr>";    $elemType = filetype($filePath)    $info.= "<td class=\"fileName\">".$elemName."</td>";    $info.= "<td>".$elemType."</td>";

$info變數儲存著整個行的資訊,下面會繼續用到。


通過下面三個函數可以得到後面三欄的值:

  1. 可讀:is_readable

  2. 可寫:is_writable

  3. 可執行:is_executable

然而,這樣得到的結果只是BOOL值,需要轉化為具體的映像連結。下面用?:三元運算元簡潔的實現了這一功能。

    $pathCorrect = "images/correct.png";    $pathError = "images/error.png";    $pathReadable = is_readable($filePath)?$pathCorrect:$pathError;    $info.= "<td class=\"sig\"><img src=\"".$pathReadable."\" alt=\"\" class=\"tabmenu\"></td>";    $pathWritable = is_writable($filePath)?$pathCorrect:$pathError;    $info.= "<td class=\"sig\"><img src=\"".$pathWritable."\" alt=\"\" class=\"tabmenu\"></td>";    $pathExcutable = is_executable($filePath)?$pathCorrect:$pathError;    $info.= "<td class=\"sig\"><img src=\"".$pathExcutable."\" alt=\"\" class=\"tabmenu\"></td>";

注意其使用的樣式類sig,對應的css為:

.sig{text-align: center;}

可以使表徵圖置中顯示。


使用filesize或stat()["size"]可以擷取基於BYTE的檔案大小,但是那樣太冗餘,因此筆者編寫了下面的sizeToBytes函數用於給出檔案的相對大小:

function sizeToBytes($size){    $listUnits = array("Bytes", "Kb", "Mb", "Gb", "Tb");    $index = 0;    $base = 1;    while ($size >= $base * 1024)    {        $base *= 1024;        $index ++;    }    return sprintf("%.2f%s", $size/$base, $listUnits[$index]);}$info.= "<td>".sizeToBytes($res["size"])."</td>";


檔案的建立、修改、訪問時間可以通過以下方式列出

    $info.= "<td>".date("Y-m-d H:m:s",$res["ctime"])."</td>";    $info.= "<td>".date("Y-m-d H:m:s",$res["mtime"])."</td>";    $info.= "<td>".date("Y-m-d H:m:s",$res["atime"])."</td>";


實際上,上面的諸多函數除了檔案大小外其他都適用於檔案夾的屬性。


具體效果:


650) this.width=650;" src="http://s3.51cto.com/wyfs02/M01/59/C6/wKioL1Thm4vA4ZbqAAJb0f6JtJ0411.jpg" title="PHP檔案管理工具5.png" alt="wKioL1Thm4vA4ZbqAAJb0f6JtJ0411.jpg" />

本文出自 “Accplayer的小地方” 部落格,請務必保留此出處http://accplaystation.blog.51cto.com/9917407/1614667

PHP伺服器檔案管理工具開發小結(五):擷取檔案屬性資訊

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.