PHP之檔案目錄基礎操作方法

2022-06-11 11:54:12 字數 2873 閱讀 5411

1.檔案的屬性資訊獲取

首先檔案具有型別,在linux下邊,有block(塊裝置,如磁碟分割槽、cd-rom)、char(以字元為輸入的裝置,如鍵盤、印表機)、dir(目錄型別,目錄也是檔案的一種)、fifo(命名管道,解釋是將資訊從乙個程序傳到另乙個程序)、file(普通的檔案)、link(鏈結,類似win下邊的快捷方式)、unknow(未知型別)7大類,在win下邊,只有3類:file、dir和unknown。linux渣表示一定要好好搞一下linux-_-,人家完全是為linux而生。

關於屬性的獲取有這麼幾個函式:

file_exists:判斷檔案或目錄是否存在;

filesize:獲取檔案大小;

is_readable、is_writable、is_executable :是否可讀、可寫、可執行;

filectime、filemtime、fileatime:獲取檔案的建立時間(create)、修改時間(modify)、訪問時間(access),均返回時間戳;

stat:獲取檔案的一些基本資訊,返回乙個索引與關聯混合陣列。

比如,可以這樣判斷檔案型別:

複製**

function getfiletype($path){ // 獲取檔案型別

switch(filetype($path)){

case 'file': return 'ordinary file';

case 'dir': return 'directory';

case 'block': return 'block device file';

case 'char': return 'transfer device base on char';

case 'fifo': return 'named pipes';

case 'link': return 'symbol link';

default: return 'unknown type';

複製**

filesize返回的是以位元組為單位的資料,如果是大檔案數字或很大,可以對數字先處理一下,**如下

複製**

<?php

// 處理檔案大小

function getsize($path = '', $size = -1){

if($path !== null && $size == -1){ // 只傳路徑就計算大小,也可以使之只處理數字

$size = filesize($path);

if($size >= pow(2, 40)){

return round($size/pow(2, 40), 2).'tb';

else if($size >= pow(2, 30)){

return round($size/pow(2, 30), 2).'gb';

else if($size >= pow(2, 20)){

return round($size/pow(2, 20), 2).'mb';

else if($size >= pow(2, 10)){

return round($size/pow(2, 10), 2).'kb';

else{

return round($size, 2).'byte';

複製**

現在綜合來獲取一下檔案資訊,**如下:

複製**

<?php

function getfileinfo($path){

if(!file_exists($path)){ // 判斷檔案是否存在

echo 'file not exists!

';return;

if(is_file($path)){ // 是檔案,列印基礎檔名

echo basename($path).' is a file

';if(is_dir($path)){ // 是目錄 ,返回目錄

echo dirname($path).' is a directory

';echo 'file type:'.getfiletype($path).'

'; // 獲取檔案型別

echo 'file size:'.getsize($path).'

'; // 獲取檔案大小

if(is_readable($path)){ // 是否可讀

echo basename($path).' is readable

';if(is_writeable($path)){ // 是否可寫

echo basename($path).' is writeable

';if(is_executable($path)){ // 是否可執行

echo basename($path).' is executable

';// touch函式可以修改這些時間

echo 'file create time: '.date('y-m-d h:i:s', filectime($path)).'

'; // 建立時間

echo 'file modify time: '.date('y-m-d h:i:s', filemtime($path)).'

'; // 修改時間

echo 'last access time: '.date('y-m-d h:i:s', fileatime($path)).'

'; // 上次訪問時間

echo 'file owner: '.fileowner($path).'

'; // 檔案擁有者

echo 'file permission: '.substr(sprintf('%o', (fileperms($path))), -4).'

'; // 檔案許可權,八進位制輸出

echo 'file group: '.filegroup($path).'

'; // 檔案所在的組

檔案 和 目錄操作方法總結

檔案操作方法 目錄操作方法 檔案操作方法 基本概念 filename就是指檔案的名字 a.txt include index v 之類的 handle就是指資源控制代碼 str fopen filename,mode str就是控制代碼,開啟檔案的鍋把兒 檔案建立 touch filename 建立...

python檔案和目錄操作方法大全

一 python中對檔案 資料夾操作時經常用到的os模組和shutil模組常用方法 1.得到當前工作目錄,即當前python指令碼工作的目錄路徑 os.getcwd 2.返回指定目錄下的所有檔案和目錄名 os.listdir 3.刪除乙個檔案 os.remove 4.刪除多個目錄 os.remove...

python檔案和目錄操作方法大全

一 python中對檔案 資料夾操作時經常用到的os模組和shutil模組常用方法。1.得到當前工作目錄,即當前python指令碼工作的目錄路徑 os.getcwd 2.返回指定目錄下的所有檔案和目錄名 os.listdir 3.函式用來刪除乙個檔案 os.remove 4.刪除多個目錄 os.re...