Check whether the file or directory exists. Return bool value
Path: required. Specifies the path to be checked.
// 查看test.txt是否存在,返回bool值
echo file_exists("target.txt"); // 1
echo "<hr>";
7: file_get_contents()
Read the file into a string .
Syntax: file_get_contents(path,include_path,context,start,max_length)
Path: required. Specifies the file to be read.
include_path: optional. Set this parameter to '1' if you also want to search for files in include_path (in php.ini).
Context: Optional. Specifies the environment for a file handle. context is a set of options that can modify the behavior of the stream. If NULL is used, it is ignored.
Start: Optional. Specifies the position in the file to begin reading. This parameter is new in PHP 5.1.
max_length: optional. Specifies the number of bytes to read. This parameter is new in PHP 5.1.
// 读取文件
echo file_get_contents("target.txt");
echo "<hr>";
Tip: This function is binary safe. (Meaning that both binary data (such as images) and character data can be written using this function.)
8: file_put_contents()
Write string to file. If successful, the function returns the number of characters written to the file. On failure, False is returned.
Syntax: int file_put_contents ( string filename,mixedfilename , mixed filename,mixeddata [, int flags=0[,resourceflags = 0 [, resource flags=0[,resourcecontext ]] )
File: required. Specifies the file to which data is to be written. If the file does not exist, a new file is created.
Data: required. Specifies the data to be written to the file. Can be a string, array or data stream.
Mode: Optional. Specifies how to open/write the file. Possible values: FILE_USE_INCLUDE_PATH/FILE_APPEND/LOCK_EX
Context: Optional. Specifies the environment for a file handle. context is a set of options that can modify the behavior of the stream.
// 写入文件
echo file_put_contents("sites.txt","Runoob");
echo "<hr>";
9 :filesize()
函数返回指定文件的大小。
如果成功,该函数返回文件大小的字节数。如果失败,则返回 FALSE。
语法:filesize(filename)
Filename:必需。规定要检查的文件。
// 返回文件大小
echo filesize("target.txt");
echo "<hr>";
10 :filetype()
函数返回指定文件或目录的类型。
若成功,则返回 7 种可能的值。若失败,则返回 false。
语法:filetype(filename)
Filename:必需。规定要检查的文件。
// 返回文件类型
echo filetype("target.txt");
echo "<hr>";
11 :glob()
返回一个包含匹配指定模式的文件名/目录的数组。
glob() 函数返回匹配指定模式的文件名或目录。
该函数返回一个包含有匹配文件 / 目录的数组。如果出错返回 false。
语法:glob(pattern,flags)
File:必需。规定检索模式。
Size:可选。规定特殊的设定。
- GLOB_MARK - 在每个返回的项目中加一个斜线
- GLOB_NOSORT - 按照文件在目录中出现的原始顺序返回(不排序)
- GLOB_NOCHECK - 如果没有文件匹配则返回用于搜索的模式
- GLOB_NOESCAPE - 反斜线不转义元字符
- GLOB_BRACE - 扩充 {a,b,c} 来匹配 'a','b' 或 'c'
- GLOB_ONLYDIR - 仅返回与模式匹配的目录项
- GLOB_ERR - 停止并读取错误信息(比如说不可读的目录),默认的情况下忽略所有错误
注释:GLOB_ERR 是 PHP 5.1 添加的。
echo "<pre class="brush:php;toolbar:false">";
var_dump(glob("*.*"));
echo "<hr>";
12 :is_dir()
判断指定的文件名是否是一个目录。
语法:is_dir(file)
File:必需。规定要检查的文件。
$file = "D:/wwwroot/xxx.com/";
if(is_dir($file))
{
echo ("$file is a directory");
}
else
{
echo ("$file is not a directory");
}
echo "<hr>";
13 :is_writable()
判断文件是否可写。如果文件存在并且可写则返回 true。
语法:is_writable(file)
File:必需。规定要检查的文件。
$file = "target.txt";
if(is_writable($file))
{
echo ("$file is writeable");
}
else
{
echo ("$file is not writeable");
}
echo "<hr>";
14 :mkdir()
创建目录,如果成功该函数返回 TRUE,如果失败则返回 FALSE。
语法:mkdir(path,mode,recursive,context)
Path:必需。规定要创建的目录的名称。
Mode:可选。规定权限。默认是 0777(允许全局访问)。
mode 参数由四个数字组成:
第一个数字通常是 0
第二个数字规定所有者的权限
第三个数字规定所有者所属的用户组的权限
第四个数字规定其他所有人的权限
可能的值(如需设置多个权限,请对下面的数字进行总计):
1 = 执行权限
2 = 写权限
4 = 读权限
Recursive:可选。规定是否设置递归模式。(PHP 5 中新增的)
Context:可选。规定文件句柄的环境。context 是一套可以修改流的行为的选项。(PHP 5 中新增的)
echo mkdir("testing");
echo "<hr>";
注释: mode 参数在 Windows 平台上被忽略。
15 :move_uploaded_file()
将上传的文件移动到新位置。若成功,则返回 true,否则返回 false。
文件上传的核心就是这个文件
语法:move_uploaded_file(file,newloc)
File:必需。规定要移动的文件。
Newloc:必需。规定文件的新位置。
注释:本函数仅用于通过 Http POST 上传的文件。
注意:如果目标文件已经存在,将会被覆盖。
16 :parse_ini_file()
函数解析一个配置文件(ini 文件),并以数组的形式返回其中的设置。
语法:parse_ini_file(file,process_sect<strong class="keylink">io</strong>ns)
File:必需。规定要检查的 ini 文件。
process_sections:可选。如果设置为 TRUE,则返回一个多维数组,包括了配置文件中每一节的名称和设置。默认是 FALSE。
echo "<pre class="brush:php;toolbar:false">";
var_dump(parse_ini_file("test.ini"));
echo "<hr>";
注:此ini文件不一定非的是php.ini,也可以是你自己的ini配置文件。
17 :realpath()
该函数删除所有符号连接(比如 '/./', '/../' 以及多余的 '/'),并返回绝对路径名。
如果失败,该函数返回 FALSE。
语法:realpath(path)
Path:必需。规定要检查的路径。
echo realpath("test.ini");
18 :rename()
rename() 函数重命名文件或目录。
如果成功,该函数返回 TRUE。如果失败,则返回 FALSE。
语法:rename(oldname,newname,context)
Oldname:必需。规定要重命名的文件或目录。
Newname:必需。规定文件或目录的新名称。
Context:可选。规定文件句柄的环境。context 是一套可以修改流的行为的选项。
echo rename("test.ini","testss.ini");
echo "<hr>";
19 :tempnam()
创建唯一的临时文件。若成功,则该函数返回新的临时文件名。若失败,则返回 false。
语法:tempnam(dir,prefix)
Dir:必需。规定创建临时文件的目录。
Prefix:必需。规定文件名的开头。
echo tempnam("D:wwwrootxxx.com","TMP0");
echo "<hr>";
注: 此方法创建的文件,如果不再需要该文件则要删除此文件,不会自动删除的。
20 :tmpfile()
建立临时文件。此函数创建的临时文件会在文件关闭后(用 fclose())或当脚本结束后自动被删除。
语法:tmpfile()
$temp = tmpfile();
fwrite($temp, "Testing, testing.");
// 将文件指针的位置倒回文件的开头。
rewind($temp);
// 从文件中读取1K数据
echo fread($temp,1024);
//This removes the file
fclose($temp);
21:unlink()
删除文件。如果成功,该函数返回 TRUE。如果失败,则返回 FALSE。
语法:unlink(filename,context)
Filename:必需。规定要删除的文件。
Context:可选。规定文件句柄的环境。context 是一套可以修改流的行为的选项。
// 如果没有text.txt文件,这样写输出的结果会报警告,测试代码,就这样了
// 实际用的时候,需要注意这个问题
$file = "test.txt";
if (!unlink($file))
{
echo ("Error deleting $file");
}
else
{
echo ("Deleted $file");
}
22 :chmod()
改变文件权限。如果成功则返回 TRUE,如果失败则返回 FALSE。
语法:chmod(file,mode)
File:必需。规定要检查的文件。
Mode:必需。规定新的权限。
mode 参数由 4 个数字组成:
第一个数字通常是 0
第二个数字规定所有者的权限
第三个数字规定所有者所属的用户组的权限
第四个数字规定其他所有人的权限
可能的值(如需设置多个权限,请对下面的数字进行总计):
1 = 执行权限
2 = 写权限
4 = 读权限
echo chmod("target.txt",0600);
echo "<hr>";
23:chown()
改变文件所有者。如果成功则返回 TRUE,如果失败则返回 FALSE。
语法:chown(file,owner)
File:必需。规定要检查的文件。
Owner:必需。规定新的所有者。可以是用户名或用户的 ID。
echo chown("target.txt","root");
echo "<hr>";
24:chgrp()
改变文件组。如果成功则返回 TRUE,否则返回 FALSE。
语法:chgrp(file,group)
File:必需。规定要检查的文件。
Group:可选。规定新的组。可以是组名或组的 ID。
echo chgrp("test.txt","admin");
echo "<hr>";