php常用的檔案操作函數參考表_PHP教程

來源:互聯網
上載者:User
不斷閱讀,你就會發現一些新內容!

表a

函數

說明

執行個體

filesize

($file)

此函數以位元組為單位返回一個檔案的大小。它用於計算一個檔案在磁碟上佔有多少空間。

// get file owner and group
echo "uid: " . fileowner('myfile.html');
echo "gid: " . filegroup('myfile.html');
?>

fileatime

($file)

filemtime

($file)

這些函數分別返回某一檔案最後被訪問及修改的時間,用於找出一個檔案在一個特定的日期後是否被修改。

// get file access/modification times
echo "last accessed on: " . date("d-m-y", fileatime('myfile.html'));

echo "last modified on: " . date("d-m-y", filemtime('myfile.html'));
?>

fileperms

($file)

此函數返回一個檔案許可,用它來檢查檔案是否可讀,可寫或可執行。

// get permissions in octal format
echo "file permissions: " . sprintf('%0', fileperms('myfile.html'));
?>

filetype

($file)

此函數返迴文件的“類型”—是否串連,目錄,特性或塊裝置,或常規檔案。在執行某項操作前,用它來檢驗檔案的本質。

// get file type
echo "file type: " . filetype('myfile.html');
?>

stat($file)

這是一個“包羅永珍”的函數,它返回一個檔案的詳細統計資料,包括它的所有者與所屬組,大小,最後修改時間,索引節點數目。如果你需要在一個單獨的函數調用中獲得全面的檔案統計資料,請使用此函數而不是前面列舉的那些函數。

// get file statistics
print_r(stat('myfile.html'));
?>

realpath

($file)

此函數將一個相對檔案路徑轉換為絕對檔案路徑,當需要找出一個檔案在磁碟上的準確位置,則使用此函數。

// get absolute path
// returns "/tmp/myfile.html"
echo "file path: " . realpath("./cook/book/http://www.cnblogs.com/myfile.html");
?>

basename

($file)

dirname

($file)

只要給定一個完整的檔案路徑,這些函數就能將其分解為各個組成部分,並分別返迴文件名和目錄。

// split directory and file names
// returns "/usr/local/bin"
echo "directory: " . dirname("/usr/local/bin/php");

// returns "php"
echo "file: " . basename("/usr/local/bin/php");
?>

file($file)

此函數將一檔案的內容讀入一數組。數組中的每一個元素代表一行檔案。此函數用於將檔案內容讀入一個變數中,以便對它進行進一步的加工。

// read file contents
$lines = file('myfile.html');

// print line by line
for($x=1; $x<=sizeof($lines); $x++) {
?echo "line $x: " . $lines[$x-1] . "n";
}
?>


http://www.bkjia.com/PHPjc/445360.htmlwww.bkjia.comtruehttp://www.bkjia.com/PHPjc/445360.htmlTechArticle不斷閱讀,你就會發現一些新內容! 表a 函數 說明 執行個體 filesize ($file) 此函數以位元組為單位返回一個檔案的大小。它用於計算一個檔案在磁...

  • 聯繫我們

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