>
PHP文件与目录操作
2021-02-06 14:02
PHP
  • 924
  • 527
  • 64
  • 51

文件的操作函数:

1.is_file判断给定文件名是否为一个正常的文件

语法:

is_file(string $filename) : bool
filename
文件的路径。

2.is_dir判断给定文件名是否是一个目录

语法:

is_dir(string $filename) : bool

3.file_exists检查文件或目录是否存在

语法:

file_exists(string $filename) : bool

4.filesize取得文件大小

语法:

filesize(string $filename) : int

5.is_readable判断给定文件名是否可读

语法:

is_readable(string $filename) : bool

6.is_writable判断给定的文件名是否可写

语法:

is_writable(string $filename) : bool

7.filectime取得文件的 inode 修改时间

语法:

filectime(string $filename) : int

8.date格式化一个本地时间/日期

语法:

date(string $format[,int $timestamp] ) : string

返回将整数 timestamp    按照给定的格式字串而产生的字符串。如果没有给出时间戳则使用本地当前时间。换句话说,timestamp    是可选的,默认值为 time()。 


9.dirname返回路径中的目录部分

语法:

dirname(string $path) : string
path
一个路径。

10.opendir打开目录句柄

语法:

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

打开一个目录句柄,可用于之后的    closedir()readdir() 和    rewinddir() 调用中。 


11.readdir从目录句柄中读取条目

语法:

readdir([resource $dir_handle]) : string

12.rewinddir倒回目录句柄

语法:

rewinddir(resource $dir_handle) : void

dir_handle 指定的目录流重置到目录的开头。 


13.closedir关闭目录句柄

语法: 

closedir([ resource $dir_handle] ) : void

关闭由 dir_handle 指定的目录流。流必须之前被    opendir() 所打开。 


14.mkdir新建目录

语法:

mkdir(string $pathname[, int $mode= 0777[,bool $recursive= false[, resource $context]]] ) : boolpathname
目录的路径。 
mode
默认的 mode 是 0777,意味着最大可能的访问权。有关 mode 的更多信息请阅读    chmod() 页面。
Note:
mode 在 Windows 下被忽略。 
注意也许想用八进制数指定模式,也就是说该数应以零打头。模式也会被当前的 umask 修改,可以用 umask() 来改变。
recursive
允许递归创建由 pathname 所指定的多级嵌套目录。
context

15.fopen打开文件或者 URL

语法:

fopen( string $filename, string $mode [, bool $use_include_path= false[, resource $context]] ) : resource

fopen() 将    filename    指定的名字资源绑定到一个流上。 


16.fseek在文件指针中定位

语法:

fseek( resource $handle,int $offset[,int $whence= SEEK_SET] ) : int

17.flock轻便的咨询文件锁定

语法:

flock(resource $handle, int $operation[, int $wouldblock] ) : bool

handle
文件系统指针,是典型地由 fopen() 创建的 resource(资源)。
operation
operation    可以是以下值之一:
如果不希望 flock() 在锁定时堵塞,则是        LOCK_NB(Windows 上还不支持)。
LOCK_SH取得共享锁定(读取的程序)。          
LOCK_EX 取得独占锁定(写入的程序。          
LOCK_UN 释放锁定(无论共享或独占)。          
wouldblock
如果锁定会堵塞的话(EWOULDBLOCK    错误码情况下),可选的第三个参数会被设置为 TRUE。(Windows 上不支持)

18.fread读取文件(可安全用于二进制文件)

语法:

fread(resource $handle,int $length) : string

19.fwrite写入文件(可安全用于二进制文件)

语法:

fwrite(resource $handle, string $string[, int $length] ) : int

20.rename重命名一个文件或目录

语法:

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

21.unlink删除文件

语法:

unlink( string $filename[,resource$context] ) : bool

22.copy拷贝文件

语法:

copy(string $source,string $dest[,resource $context] ) : bool

23.file_get_contents将整个文件读入一个字符串

语法:

file_get_contents(string $filename[, bool $use_include_path= false[, resource $context[, int $offset= -1[, int$maxlen]]]] ) : string

24.file_put_contents将一个字符串写入文件

语法:

file_put_contents(string $filename,mixed $data[,int $flags= 0[, resource $context]] ) : int

25.readfile输出文件

语法:

readfile( string $filename[,bool $use_include_path = false[, resource $context ]] ) : int
读取文件并写入到输出缓冲。

26.rmdir删除目录

语法:

rmdir(string $dirname[,resource $context] ) : bool

演示的示列文件已上传到CYBLOG资源中: https://cyimt.net/Download/Download?Down=061400066723 



           

全部留言 ()
返回
顶部