php檔案系統處理方法總結

來源:互聯網
上載者:User
這篇文章主要介紹了php檔案系統處理方法,總結分析了php常用的檔案操作函數功能與具體使用方法,具有一定參考借鑒價值,需要的朋友可以參考下

本文總結分析了php檔案系統處理方法。分享給大家供大家參考,具體如下:

檔案類型

以Linux為模型的, 在Windows只能擷取file, dir或unknow 三種類型
在Linux/Unix下, block, char, dir, fifo, file, link, unknown7種型

block :塊設定檔案,磁碟分割,軟碟機, cd-rom等
char: 字元裝置,I/O (輸入輸出中)以字元為單位的裝置, 例如鍵盤,印表機等
dir: 目錄也是檔案的一種/目錄檔案
fifo: 資訊管道,從一個程式傳輸到另一個進程
file: 普通的檔案類型如文字檔,可執行檔
link: 連結檔案,相當於windows下的捷徑
unknown :未知類型

1.檔案屬性處理函數

filetype("目錄或檔案名稱") 擷取類型
is_dir -- 判斷給定檔案名稱是否是一個目錄
is_file -- 判斷給定檔案名稱是否為一個正常的檔案
is_link -- 判斷給定檔案名稱是否為一個符號串連
is_executable(); -- 判斷給定檔案名稱是否可執行
file_exists();--檔案是否存在
filesize();--返迴文件大小
is_readable();--檔案是否可讀
is_writeable();--檔案是否可寫
filectime();--檔案建立時間
filemtime();--檔案修改時間
fileactime();--檔案最後訪問時間
stat();--檔案狀態,返回關於給定檔案的資訊的數組

bool ftruncate ( resource handle, int size );

接受檔案指標 handle 作為參數,並將檔案大小截取為 size。如果成功則返回 TRUE,失敗則返回 FALSE。

bool rename ( string oldname, string newname [, resource context] );

2.目錄

目錄屬性

* basename(url[,副檔名]);   //返迴文件名* dirname(url);   //目錄名* pathinfo(url);  //路徑資訊

例子:

$path="/var/www/html/page.php";echo basename($path);// 返回page.phpecho basename($path,".php"); //pageecho dirname($paht);// /var/www/html$arr=pathinfo($paht);$arr["dirname"] // /var/www/html$arr["basename"]// page.php$arr["extension"]// .php

遍曆目錄

opendir(url);readdir(url);//返回目前的目錄指標只為的一個檔案名稱,並將目錄指標向後移動一位closedir(url);rewinddir(url);//把目錄指標重設到開始處

統計目錄大小

統計目錄的大小隻能建立遞迴函式把目錄的檔案都加起來;

統計磁碟大小可以使用 disk_free_space(url);和 disk_total_space(url);

建立和刪除目錄

mkdir(url);//建立目錄rmdir(url);//刪除空目錄unlink(url);//刪除檔案

刪除非空目錄只能自己建立遞迴函式;

複製目錄

copy($scrfile,$to);//複製檔案

得自訂遞迴函式實現目錄複寫功能

3.檔案的基本操作

fopen(url);fclose(url);

寫入檔案

int fwrite(resoure handle,strint string[,int length]);

返回寫入的字元數或是FALSE

fputs()是fwrite()的別名

int file_put_contents ( string filename, string data [, int flags [, resource context]] );

和依次調用 fopen(),fwrite() 以及 fclose() 功能一樣。

讀取檔案

string fread ( resource handle, int length );

從檔案指標 handle 讀取最多 length 個位元組。 該函數在讀取完 length 個位元組數,或到達 EOF 的時候

string file_get_contents ( string filename [, bool use_include_path [, resource context [, int offset [, int maxlen]]]] );array file ( string filename [, int use_include_path [, resource context]] );

數組中的每個單元都是檔案中相應的一行,包括分行符號在內。

string fgets ( resource handle [, int length] );string fgetc ( resource handle );int readfile ( string filename [, bool use_include_path [, resource context]] );

讀入一個檔案並寫入到輸出緩衝。

如果訪問遠程檔案,必須在php的設定檔中啟用"allow_url_fopen"選項,才能使用fopen()函數開啟遠程檔案

使用FTP協議串連遠程檔案的時,只可以用“唯讀”或“唯寫”模式開啟檔案。

移動檔案指標

int ftell ( resource handle );

返回由 handle 指定的檔案指標的位置,也就是檔案流中的位移量。

int fseek ( resource handle, int offset [, int whence] );

在與 handle 關聯的檔案中設定檔案指標位置。新位置,從檔案頭開始以位元組數度量,是以 whence 指定的位置加上 offset。whence de 值定義為:

SEEK_SET - 設定位置等於 offset 位元組。
SEEK_CUR - 設定位置為當前位置加上 offset。
SEEK_END - 設定位置為檔案尾加上 offset。(要移動到檔案尾之前的位置,需要給 offset 傳遞一個負值。)

bool rewind ( resource handle );

將 handle 的檔案位置指標設為檔案流的開頭

總結:以上就是本篇文的全部內容,希望能對大家的學習有所協助。

聯繫我們

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