Home >Backend Development >PHP Tutorial >Introduction to php folder and file directory operation functions_PHP tutorial
php文件夹操作函数
string basename ( string path [, string suffix] )
给出一个包含有指向一个文件的全路径的字符串,本函数返回基本的文件名。如果文件名是以 suffix 结束的,那这一部分也会被去掉。
在 Windows 中,斜线(/)和反斜线()都可以用作目录分隔符。在其它环境下是斜线(/)。
string dirname ( string path )
给出一个包含有指向一个文件的全路径的字符串,本函数返回去掉文件名后的目录名。
在 Windows 中,斜线(/)和反斜线()都可以用作目录分隔符。在其它环境下是斜线(/)。
array pathinfo ( string path [, int options] )
pathinfo() 返回一个联合数组包含有 path 的信息。包括以下的数组单元:dirname,basename 和 extension。
可以通过参数 options 指定要返回哪些单元。它们包括:PATHINFO_DIRNAME,PATHINFO_BASENAME 和 PATHINFO_EXTENSION。默认是返回全部的单元。
string realpath ( string path )
realpath() 扩展所有的符号连接并且处理输入的 path 中的 ‘/./', ‘/../' 以及多余的 ‘/' 并返回规范化后的绝对路径名。返回的路径中没有符号连接,'/./' 或 ‘/../' 成分。
realpath() 失败时返回 FALSE,比如说文件不存在的话。在 BSD 系统上,如果仅仅是 path 不存在的话,PHP 并不会像其它系统那样返回 FALSE。
bool is_dir ( string filename )
如果文件名存在并且为目录则返回 TRUE。如果 filename 是一个相对路径,则按照当前工作目录检查其相对路径。
注: 本函数的结果会被缓存。更多信息参见 clearstatcache()。
resource opendir ( string path [, resource context] )
打开一个目录句柄,可用于之后的 closedir(),readdir() 和 rewinddir() 调用中。
string readdir ( resource dir_handle )
返回目录中下一个文件的文件名。文件名以在文件系统中的排序返回。
void closedir ( resource dir_handle )
关闭由 dir_handle 指定的目录流。流必须之前被 opendir() 所打开。
void rewinddir ( resource dir_handle )
将 dir_handle 指定的目录流重置到目录的开头。
array glob ( string pattern [, int flags] )
glob() 函数依照 libc glob() 函数使用的规则寻找所有与 pattern 匹配的文件路径,类似于一般 shells 所用的规则一样。不进行缩写扩展或参数替代。
返回一个包含有匹配文件/目录的数组。如果出错返回 FALSE。
有效标记为:
GLOB_MARK - 在每个返回的项目中加一个斜线
GLOB_NOSORT - 按照文件在目录中出现的原始顺序返回(不排序)
GLOB_NOCHECK - 如果没有文件匹配则返回用于搜索的模式
GLOB_NOESCAPE - 反斜线不转义元字符
GLOB_BRACE - 扩充 {a,b,c} 来匹配 ‘a','b' 或 ‘c'
GLOB_ONLYDIR - 仅返回与模式匹配的目录项
注: 在 PHP 4.3.3 版本之前 GLOB_ONLYDIR 在 Windows 或者其它不使用 GNU C 库的系统上不可用。
GLOB_ERR - 停止并读取错误信息(比如说不可读的目录),默认的情况下忽略所有错误
注: GLOB_ERR 是 PHP 5.1 添加的。
php文件目录操作
新建文件
1、先确定要写入文件的内容
$content = '你好';
2、打开这个文件(系统会自动建立这个空文件)
//假设新建的文件叫file.txt,而且在上级目录下。w表示‘写文件',$fp下面要用到,表示指向某个打开的文件。
$fp = fopen('../file.txt', 'w');
3、将内容字符串写入文件
//$fp告诉系统要写入的文件,写入的内容是$content
fwrite($fp, $content);
4、关闭文件
fclose($fp);
说明:PHP5中提供了更方便的函数file_put_contents,上面的4步可以这样完成:
$content = '你好';
file_put_contents('file.txt',$content);
删除文件
//删除当前目录下的arch目录下的文件abc.txt
unlink('arch/abc.txt');
说明:系统会返回操作结果,成功则返回 TRUE,失败则返回 FALSE,可以用变量接收,就知道是否删除成功:
$deleteResult = unlink('arch/abc.txt');
获取文件内容
//假设获取的目标文件名是file.txt,而且在上级目录下。获取的内容放入$content。
$content = file_get_contents('../file.txt');
修改文件内容
操作方法与新建内容基本一样
Rename file or directory
//Rename file 1.gif under subdirectory a in the current directory to 2.gif.
rename('/a/1.gif', '/a/2.gif');
Note: The same is true for directories. The system will return the operation result, TRUE if successful, and FALSE if failed. You can use a variable to receive it to know whether the rename is successful.
$renameResult = rename('/a/1.gif', '/a/2.gif');
If you want to move a file or directory, just set the renamed path to the new path. That’s it:
//Move the file 1.gif under subdirectory a in the current directory to subdirectory b under the current directory, and rename it to 2.gif.
rename('/a/1.gif', '/b/2.gif');
However, please note that if directory b does not exist, the move will fail.
Copy file
//Copy the file 1.gif in subdirectory a of the current directory to subdirectory b of the current directory and name it 2.gif.
copy('/a/1.gif', '/b/1.gif');
Note: This operation cannot be performed on the directory.
If the target file (/b/1.gif above) already exists, the original file will be overwritten.
The system will return the operation result, TRUE if successful, and FALSE if failed. You can use a variable to receive it to know whether the copy was successful.
$copyResult = copy('/a/1.gif', '/b/1.gif');
Moving files or directories
The operation method is the same as renaming
Whether the file or directory exists
//Check whether the file logo.jpg in the upper-level directory exists.
$existResult = file_exists('../logo.jpg');
Description: The system returns true if the file exists, otherwise it returns false. The same operation can be done with directories.
Get the file size
//Get the size of the file logo.png in the upper directory.
$size = filesize('../logo.png');
Explanation: The system will return a number indicating the size of the file in bytes.
New directory
//Create a new directory b below directory a in the current directory.
mkdir('/a/b');
Explanation: The system will return the operation result. TRUE if successful, FALSE if failed. You can use a variable to receive it to know whether the new creation is successful:
$mkResult = mkdir('/a/b');
Delete Directory
//Delete subdirectory b below directory a in the current directory.
rmdir('/a/b');
Note: Only non-empty directories can be deleted, otherwise the subdirectories and files under the directory must be deleted first, and then the total directory
The system will return the operation results , returns TRUE if successful, and FALSE if failed. You can use a variable to receive it to know whether the deletion is successful:
$deleteResult = rmdir('/a/b');
Get all file names in the directory
1. First open the directory to be operated and point a variable to it
//Open the subdirectory common under the directory pic in the current directory.
$handler = opendir('pic/common');
2. Loop to read all files in the directory
/*where $filename = readdir($handler) is the The read file name is assigned to $filename. In order not to get stuck in an infinite loop, $filename !== false is also required. Be sure to use !==, because if a file name is called '0', or something is considered false by the system, using != will stop the loop */
while( ($filename = readdir($ handler)) !== false ) {
3. There will be two files in the directory, named '.' and '..', do not operate them
if($filename != "." && $filename != "..") {
4. Process
//Here we simply use echo to output the file name
echo $filename;
}
}
5. Close the directory
closedir($handler);
Whether the object is a directory
//Check whether the target object logo.jpg in the upper-level directory is a directory.
$checkResult = is_dir('../logo.jpg');
Description: If the target object is a directory system, return true, otherwise return false. Of course $checkResult in the above example is false.
Whether the object is a file
//Check whether the target object logo.jpg in the upper-level directory is a file.
$checkResult = is_file('../logo.jpg');
Note: If the target object is a file, the system returns true, otherwise it returns false. Of course $checkResult in the above example is true.