php 操作系统,PHP 处理文件和操作系统

1 文件与目录

1.1 解析目录路径

1) 获取路径的文件

string basename(string path, [, string suffix])

如果提供了可选参数suffix,当返回的文件名包含这个扩展名时将忽略该后缀。

2) 获取路径的目录

string dirname(string path)

返回路径的目录部分。

3) 更多关于路径的信息

array pathinfo(string path [, options])

返回一个关联数组,其中包括路径的四部分信息:目录名、基本名、扩展名和文件名

$pinfo = pathinfo(“/path/to/index.html”);

$pinfo[‘dirname’]

$pinfo[‘basename’]

$pinfo[‘extension’]

$pinfo[‘filename’]

4) 确定绝对路径

string realpath(string path)

将path中的所有符号链接和相对路径引用转换为相应的硬链接和绝对路径。

1.2 计算文件、目录和磁盘大小

1) 确定文件的大小

int filesize(string filename)返回指定文件的大小,以字节为单位。

2) 计算磁盘的可用空间

float disk_free_space(string directory)返回指定目录所在磁盘分区的可用空间,以字节为单位。

3) 计算磁盘的总容量

float disk_total_space(string directory)返回指定目录所在的磁盘分区的总容量,以字节为单位。

4) 获取目录大小

function directorySize($directory){

$directorySize = 0;

if($dh = @opendir($directory)){

while($filename != “.” && $filename != “..”){

if(is_file($directory.”/”.$filename))

$directorySize += filesize($directory.”/”.$filename);

if(is_dir($directory.”/”.$filename))

$directorySize += directorySize($directory.”/”.$filename);

}

}

}

1.3 确定访问和修改时间

1) 确定文件的最后访问时间

int fileatime(string filename)返回文件最后的访问时间,采用UNIX时间戳格式。

2) 确定文件最后改变时间

int filectiome(string filename)

注:最后改变时间是对inode的相关信息修改的最后时间

3) 确定文件的最后修改时间

int filemtime(string filename) 返回文件的最后修改时间。

2 文件处理

资源(resource)这个词常与可以发起输入或输出流的实体联系在一起。标准输入或输出、文件和网络套接字都是资源的例子。

换行符通过字符\n表示,表示文件中一行的末尾。

程序需要一种标准的方式来识别合适到达文件的末尾。这个标准通常称为文件末尾(或EOF)字符。在PHP中,feof()函数用来确定是否到达资源末尾,它在文件IO操作中经常使用。

2.1 打开和关闭文件

1) 打开文件

resource fopen(string resource, string mode [, int use_include_path [, resource context]])

文件打开模式定义:

R 只读

r+ 读写

W 只写。删除全部已有内容。

w+ 读写。

A 只写,向文件追加内容

x 创建并打开只写的文件。

x+ 创建并以读写方式打开文件

如果use_include_path为1,则PHP考虑配置指令include_path中指定的路径。

2) 关闭文件

boolean fclose(resource filehandle)

filehandle必须是使用fopen()或fsockopen()打开的已存在的文件指针。

2.2 读取文件

1) 将文件读入数组

array file(string filename [int use_include_path [, resource context]]) 能够将文件读取到数组中,换行符附加在每个元素的末尾。

注意:file()很特殊,它不像其它读写函数,它不必建立文件句柄来读取文件内容。

2) 将文件内容读入字符串变量

string file_get_contents(string filename [int use_include_path [, resource context [, int offset [, int maxlen]]]]) 将文件中的内容读到字符串中。

3 将CSV文件读入数组

array fgetcsv(resource handle [, int length [, string delimiter [, string enclosure]]])

省略length或将其设为0都会导致任意的行长度。可以设置一个比最大行大的数值用来提升性能。

file()和list()可以实现fgetcsv()函数的功能

[code]

$users = file(“/usr/data/s.csv”);

foreach($users as $user){

list($name, $email, $phone) = explode(‘,’, $user);

….

}

[/code]

这种方式客观性更强一些。使用fgetcsv()时如果希望遇到换行符停止,那么不能指定长度,否则不可控。

4 读取指定书目的字符

string fgets(resource handel [, int length])

从 handle 指向的文件中读取一行并返回长度最多为 length – 1 字节的字符串。碰到换行符(包括在返回值中)、EOF 或者已经读取了 length – 1 字节后停止(看先碰到那一种情况)。如果没有指定 length,则默认为 1K,或者说 1024 字节。

[code]

$h = fopen(‘t.txt’,’r’);

$i=1;

while(!feof($h)){

echo $i.’->’.fgets($h, 10);

$i++;

}

fclose($h);

[/code]

从以上程序可以测试出,每次读取9个字符,但是如果先遇到换行或EOF,则忽略长度限制。如果把length设置的比较大,则可以看做是读取文件行函数,当然它有移动文件位置指针。

5 从输入中剔除标签

string fgetss(resource handle, int length [, string allowable_tags])

和fgets()函数类似,只是它从输入中清除所有HTML和PHP标签。如果要忽略某些标签,就把其放入allowable_tags中。参数 length 从 PHP 5 起开始可选。

[code]

$tags = ‘

’;

$fh = fopen(“a.html”,”r”);

while(!feof($fh)){

$article .= fgetss($fh, 1024, $tags);

}

fclose($fh);

$fh = fopen(“a.html”, “w”);

fwrite($fh, $article);

fclose($fh);

[/code]

6 以一次读取一个字符的方式读取文件

stirng fgetc(resource handel)

7 忽略换行符

string fread(resource handle, int length)

fread不考虑换行符,当读取了length个字节或到达了EOF则停止,它有助于读取二进制文件。只要使用filesize()确定了应当读取的字符数,就能很方便使用这个函数读取整个文件。

[code]

$file = ‘/var/data/users.txt’;

$fh = fopen($file, ‘r’);

$userdata = fread($fh, filesize($file));

fclose($fh);

filesize()返回文件有多少个字节。

[/code]

8 读取整个文件

int readfile(string filename [, int use_include_path]) 读取由filename指定的整个文件,立即输出到缓冲区并返回读取的字节数。

9 根据预定义的格式读取文件

mixed fscanf(resource handle, string format [, string var1]) 按照预定义的格式解析资源

2.3 将字符串写入文件

int fwrite(resource handle ,string string [, int length]) 将字符串的内容输出到指定的资源中。

2.4 移动文件指针

1) 将文件指针移到偏移量指定的位置

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

2) 获取当前指针的偏移量

int ftell(resource handle)

3) 将文件指针移回至文件开始处

int rewind(resource handle)

2.5 读取目录内容

1) 打开目录句柄

resource opendir(string path, [, resource context])

2) 关闭目录句柄

void closedir(resource directory_handle)

3) 解析目录内容

string readdir(resource dir_handle) 返回目录中下一个文件的文件名。文件名以在文件系统中的排序返回。

4) 将目录读入数组

array scandir(string directory [, int sorting_order [, resource context]])

3 执行shell命令

1) 删除目录

int rmdir(string dirname)

2) 重命名文件

boolean rename(string oldname, string newname [, resource context])

3 touch文件

int touch(string filename [, int time [, int atime]]) 文件不存在将创建空文件

4 系统级程序执行

4.1 清理输入

string escapeshellarg(string arguments) 函数用单引号界定给定的参数,并为输入的参数中的单引号进行转义。

string escapeshellcmd(string command)

4.2 PHP的程序执行函数

1) 执行系统级命令

string exec(string command [, array &output [, &retrun_var]])

2) 获取系统命令的结果

string system(string command, [, int return_var]) 函数直接将输出返回给调用者。

3) 返回二进制输出

void passthru(string command [, int &return_var])

4) 用反引号执行shell命令

使用反引号界定字符串时,就是告诉PHP该字符串应当作为shell命令来执行。

5 可代替反引号的函数

string shell_exec(string command)

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值