The following text:
php folder operation function
string basename ( string path [, string suffix] )
Given a string containing the full path to a file, this function returns the basic file name. If the filename ends with suffix, this part will also be removed.
In Windows, both slash (/) and backslash () can be used as directory separators. In other circumstances it is a slash (/).
string dirname ( string path )
Given a string containing the full path to a file, this function returns the directory name after removing the file name.
In Windows, both slash (/) and backslash () can be used as directory separators. In other circumstances it is a slash (/).
array pathinfo ( string path [, int options] )
pathinfo() returns an associative array containing path information. Includes the following array elements: dirname, basename, and extension.
You can specify which units to return through the parameter options. They include: PATHINFO_DIRNAME, PATHINFO_BASENAME and PATHINFO_EXTENSION. The default is to return all units.
string realpath ( string path )
realpath() expands all symbolic links and handles '/./', '/../' and redundant characters in the input path '/' and returns the normalized absolute pathname. There are no symbolic links, '/./' or '/../' components in the returned path.
realpath() returns FALSE when it fails, for example, if the file does not exist. On BSD systems, if path simply does not exist, PHP will not return FALSE like other systems.
bool is_dir ( string filename )
Returns TRUE if the filename exists and is a directory. If filename is a relative path, its relative path is checked against the current working directory.
Note: The results of this function will be cached. See clearstatcache() for more information.
resource opendir ( string path [, resource context] )
Opens a directory handle that can be used in subsequent closedir(), readdir() and rewinddir() calls.
string readdir ( resource dir_handle )
Returns the file name of the next file in the directory. File names are returned in order in the file system.
void closedir ( resource dir_handle )
Close the directory stream specified by dir_handle. The stream must have been previously opened by opendir().
void rewinddir ( resource dir_handle )
Reset the directory stream specified by dir_handle to the beginning of the directory.
array glob ( string pattern [, int flags] )
The glob() function finds all file paths that match pattern according to the rules used by the libc glob() function, similar to the rules used by ordinary shells. No abbreviation expansion or parameter substitution is performed.
Returns an array containing matching files/directories. Returns FALSE if an error occurs.
Valid markers are:
GLOB_MARK - Add a slash to each returned item
GLOB_NOSORT - Return the files in the original order they appear in the directory (not sorted)
GLOB_NOCHECK - Returns the pattern used to search if no file matches
GLOB_NOESCAPE - Backslash does not escape metacharacters
GLOB_BRACE - Expands {a,b,c} to match 'a', 'b' or 'c'
GLOB_ONLYDIR - Return only directory entries matching the pattern
Note: Before PHP 4.3.3, GLOB_ONLYDIR was not available on Windows or other systems that do not use the GNU C library.
GLOB_ERR - Stop and read error messages (such as unreadable directories), ignore all errors by default
Note: GLOB_ERR was added in PHP 5.1.
php file directory operation
New file
1. First determine the content to be written to the file
$content = '你好';
2.Open this file ( The system will automatically create this empty file)
//假设新建的文件叫file.txt,而且在上级目录下。w表示‘写文件',$fp下面要用到,表示指向某个打开的文件。 $fp = fopen('../file.txt', 'w');
3. Write the content string into the file
//$fp告诉系统要写入的文件,写入的内容是$content fwrite($fp, $content);
4. Close the file
fclose($fp);
Note: PHP5 provides a more convenient function file_put_contents. The above 4 steps can be completed as follows:
$content = '你好'; file_put_contents('file.txt',$content);
Delete the file
//删除当前目录下的arch目录下的文件abc.txt unlink('arch/abc.txt');
Note: The system will Returns the operation result. TRUE if successful, FALSE if failed. You can use a variable to receive it to know whether the deletion is successful:
$deleteResult = unlink('arch/abc.txt');
Get the file content
//假设获取的目标文件名是file.txt,而且在上级目录下。获取的内容放入$content。 $content = file_get_contents('../file.txt');
Modify the file Content
The operation method is basically the same as creating new content
Renaming files or directories
//将当前目录下的子目录a下面的文件1.gif重命名为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:
//将当前目录下的子目录a下面的文件1.gif,移动到当前目录下的子目录b,并且重命名为2.gif。 rename('/a/1.gif', '/b/2.gif');
But be careful, If directory b does not exist, the move will fail.
Copy files
//将当前目录下的子目录a下面的文件1.gif,复制到当前目录下的子目录b,并命名为2.gif。 copy('/a/1.gif', '/b/1.gif');
Explanation: 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
//检查上级目录下的文件logo.jpg是否存在。 $existResult = file_exists('../logo.jpg');
Instructions: If the file exists in the system Returns true, otherwise returns false. The same operation can be done with directories.
Get the file size
//获取上级目录下的文件logo.png的大小。 $size = filesize('../logo.png');
Instructions: The system will return a number indicating the size of the file in bytes.
New directory
//在当前目录下的目录a下面新建目录b。 mkdir('/a/b');
说明:系统会返回操作结果,成功则返回 TRUE,失败则返回 FALSE,可以用变量接收,就知道是否新建成功:
$mkResult = mkdir('/a/b');
删除目录
//删除当前目录下的目录a下面的子目录b。 rmdir('/a/b');
说明:只能删除非空的目录,否则必须先删除目录下的子目录和文件,再删除总目录
系统会返回操作结果,成功则返回 TRUE,失败则返回 FALSE,可以用变量接收,就知道是否删除成功:
$deleteResult = rmdir('/a/b');
获取目录中的所有文件名
1、先打开要操作的目录,并用一个变量指向它
//打开当前目录下的目录pic下的子目录common。 $handler = opendir('pic/common');
2、循环的读取目录下的所有文件
/*其中$filename = readdir($handler)是每次循环的时候将读取的文件名赋值给$filename, 为了不陷于死循环,所以还要让$filename !== false。一定要用!==,因为如果某个文件名如果叫'0',或者某些被系统认为是代表false,用!=就会停止循环*/ while( ($filename = readdir($handler)) !== false ) { 3、目录下都会有两个文件,名字为'.'和‘..',不要对他们进行操作 if($filename != "." && $filename != "..") { 4、进行处理 //这里简单的用echo来输出文件名 echo $filename; } }
5、关闭目录
closedir($handler);
对象是否是目录
//检查上级目录下的目标对象logo.jpg是否是目录。 $checkResult = is_dir('../logo.jpg');
说明:如果目标对象是目录系统返回true,否则返回false。上面例子的$checkResult当然是false。
对象是否是文件
//检查上级目录下的目标对象logo.jpg是否是文件。 $checkResult = is_file('../logo.jpg');
说明:如果目标对象是文件,系统返回true,否则返回false。上面例子的$checkResult当然是true。
以上就是(进阶篇)php文件夹与文件目录操作函数介绍的内容,更多相关内容请关注PHP中文网(www.php.cn)!