Home  >  Article  >  Backend Development  >  php文件系统处理方法小结,_PHP教程

php文件系统处理方法小结,_PHP教程

WBOY
WBOYOriginal
2016-07-12 08:50:43848browse

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.php
echo basename($path,".php"); //page
echo 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 的文件位置指针设为文件流的开头

更多关于PHP相关内容感兴趣的读者可查看本站专题:《php文件操作总结》、《PHP运算与运算符用法总结》、《PHP网络编程技巧总结》、《PHP基本语法入门教程》、《php操作office文档技巧总结(包括word,excel,access,ppt)》、《php日期与时间用法总结》、《php面向对象程序设计入门教程》、《php字符串(string)用法总结》、《php+mysql数据库操作入门教程》及《php常见数据库操作技巧汇总》

希望本文所述对大家PHP程序设计有所帮助。

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1133092.htmlTechArticlephp文件系统处理方法小结, 本文总结分析了php文件系统处理方法。分享给大家供大家参考,具体如下: 文件类型 以Linux为模型的, 在Win...
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn