


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.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics











PHP is a scripting language widely used on the server side, especially suitable for web development. 1.PHP can embed HTML, process HTTP requests and responses, and supports a variety of databases. 2.PHP is used to generate dynamic web content, process form data, access databases, etc., with strong community support and open source resources. 3. PHP is an interpreted language, and the execution process includes lexical analysis, grammatical analysis, compilation and execution. 4.PHP can be combined with MySQL for advanced applications such as user registration systems. 5. When debugging PHP, you can use functions such as error_reporting() and var_dump(). 6. Optimize PHP code to use caching mechanisms, optimize database queries and use built-in functions. 7

PHP and Python each have their own advantages, and the choice should be based on project requirements. 1.PHP is suitable for web development, with simple syntax and high execution efficiency. 2. Python is suitable for data science and machine learning, with concise syntax and rich libraries.

PHP and Python each have their own advantages, and choose according to project requirements. 1.PHP is suitable for web development, especially for rapid development and maintenance of websites. 2. Python is suitable for data science, machine learning and artificial intelligence, with concise syntax and suitable for beginners.

PHP is widely used in e-commerce, content management systems and API development. 1) E-commerce: used for shopping cart function and payment processing. 2) Content management system: used for dynamic content generation and user management. 3) API development: used for RESTful API development and API security. Through performance optimization and best practices, the efficiency and maintainability of PHP applications are improved.

PHP is still dynamic and still occupies an important position in the field of modern programming. 1) PHP's simplicity and powerful community support make it widely used in web development; 2) Its flexibility and stability make it outstanding in handling web forms, database operations and file processing; 3) PHP is constantly evolving and optimizing, suitable for beginners and experienced developers.

PHP is mainly procedural programming, but also supports object-oriented programming (OOP); Python supports a variety of paradigms, including OOP, functional and procedural programming. PHP is suitable for web development, and Python is suitable for a variety of applications such as data analysis and machine learning.

PHP is suitable for web development, especially in rapid development and processing dynamic content, but is not good at data science and enterprise-level applications. Compared with Python, PHP has more advantages in web development, but is not as good as Python in the field of data science; compared with Java, PHP performs worse in enterprise-level applications, but is more flexible in web development; compared with JavaScript, PHP is more concise in back-end development, but is not as good as JavaScript in front-end development.

PHP and Python have their own advantages and disadvantages, and the choice depends on project needs and personal preferences. 1.PHP is suitable for rapid development and maintenance of large-scale web applications. 2. Python dominates the field of data science and machine learning.
