


PHP file, folder (directory) operation function summary_PHP tutorial
Summary of PHP file and folder (directory) operation functions
This article will give you a summary of some commonly used folder/file directory operation functions in PHP. These Just a brief introduction to some basic methods as a note.
1. Create directory (mkdir)
bool mkdir (string $pathname [,int $mode [,bool $recursive [,resource $context ]]] )
<?php mkdir("/path/to/my/dir", 0777); //成功返回true,失败返回false;
2. Determine whether the file exists (file_exist)
bool file_exists (string $filename )
<?php $filename = '/path/to/phpernote.txt'; echo file_exists($filename)?'文件存在':'文件不存在';
3. Check whether the specified file is a directory, which is generally used to determine whether the directory exists (is_dir)
bool is_dir (string $filename )
<?php var_dump(is_dir('a_file.txt'));// 输出false var_dump(is_dir('wwwroot/phpernote')); //相对当前目录检查wwwroot/phpernote目录是否存在,存在输出true,不存在输出false var_dump(is_dir('..')); //输出true
Note: The result of this function will be cached. Please use clearstatcache() to clear the cache.
4. Determine whether the given file name is a normal file (is_file)
bool is_file ( string $filename )
<?php var_dump(is_file('a_file.txt'));//true var_dump(is_file('/usr/bin/'));//false
5. Lock or release files (flock)
bool flock ( string $filename, string $lock [,mix $block] )
The lock parameter can be one of the following values:
To obtain a shared lock (reading program), set lock to LOCK_SH (set to 1 in versions prior to PHP 4.0.1).
To obtain an exclusive lock (writing program), set lock to LOCK_EX (set to 2 in versions prior to PHP 4.0.1).
To release a lock (whether shared or exclusive), set lock to LOCK_UN (set to 3 in versions prior to PHP 4.0.1).
If you do not want flock() to block on lock, add LOCK_NB to lock (set to 4 in versions prior to PHP 4.0.1).
block optional. If set to 1 or true, blocks other processes while locking.
Tip: You can use fclose() to release the lock operation, which will be automatically called when the code is executed. For example:
<?php $file = fopen("test.txt","w+"); // 排它性的锁定 if (flock($file,LOCK_EX)){ if(is_writable($file)) fwrite($file,"www.phpernote.com 总结的文章"); // 解锁 flock($file,LOCK_UN); }else{ echo "锁定文件失败!"; } fclose($file);
6. Determine whether the given file name is a symbolic link (is_link)
bool is_link ( string $filename )
<?php var_dump(is_link("a.lnk")); //输出true
Note: The result of this function will be cached. Please use clearstatcache() to clear the cache.
7. Delete directory (rmdir) This function only deletes empty directories (rmdir)
bool rmdir ( string $dirname )
<?php var_dump(rmdir("/usr/local/a")); //当a为空目录删除成功,a为非空目录删除失败
8. Delete files (unlink)
bool unlink ( string $filename )
<?php while(is_file($wwwphpernotecom) == TRUE){ chmod($wwwphpernotecom, 0666);//设置可读取,可写入权限 unlink($wwwphpernotecom); }
9. Obtain permissions for files or directories (fileperms)
mix fileperms ( filename )
<?php //若成功,则返回文件的访问权限。若失败,则返回 false echo fileperms("test.txt");//输出:33206
Return permissions as octal value
<?php echo substr(sprintf("%o",fileperms("test.txt")),-4);//输出:1777
Tip: The result of this function will be cached. Please use clearstatcache() to clear the cache.
10. Get the type of the specified file or directory (filetype)
mix filetype ( filename )
If successful, return 7 possible values (fifo char dir block link file unknown). On failure, returns false. For example:
<?php echo filetype("test.txt");//输出:file
Tip: The result of this function will be cached. Please use clearstatcache() to clear the cache.
11. Read directory files (opendir readir closedir)
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 filename 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 )
Resets the directory stream specified by dir_handle to the beginning of the directory.
The following is a complete example of reading a directory file:
<?php $dir = "/etc/php5/"; if (is_dir($dir)) { if ($dh = opendir($dir)) { while (($file = readdir($dh)) !== false) { echo "文件名: $file : 文件类型: " . filetype($dir . $file) . "\n"; } closedir($dh); } }
12. Rename files or directories (rename)
bool rename ( oldname, newname, context )
<?php //将当前目录下的子目录a下面的文件1.gif重命名为2.gif rename('/a/1.gif', '/a/2.gif');
Note: The same goes for directories. The system will return the operation result, TRUE if successful, and FALSE if failed.
If you want to move a file or directory, just set the renamed path to the new path, for example:
<?php //将当前目录下的子目录a下面的文件1.gif,移动到当前目录下的子目录b,并且重命名为2.gif rename('/a/1.gif', '/b/2.gif'); //注意:如果目录b不存在,就会移动失败
13. Copy (copy) files (copy)
bool copy (source, destination)
<?php //将当前目录下的子目录a下面的文件1.gif,复制到当前目录下的子目录b,并命名为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; if you want to move the file, please use the rename() function.
14. Get the free space of the directory (disk_free_space)
disk_free_space ( directory )
<?php echo disk_free_space("C:");//输出:209693288558
15. Determine whether the specified file is writable (is_writable or is_writeable)
bool is_writable ( file )
说明:如果文件存在并且可写则返回 true;file 参数可以是一个允许进行是否可写检查的目录名;本函数的结果会被缓存。请使用 clearstatcache() 来清除缓存。例如:
<?php $file = "test.txt"; //或者:$file = 'd:\wwwroot\phpernote\'; echo is_writable($file)?'可写':'不可写';
16、以读写(w+)模式建立一个具有唯一文件名的临时文件(tmpfile)
resource tmpfile()
<?php $temp = tmpfile(); fwrite($temp, "Testing, www.phpernote.com"); //倒回文件的开头 rewind($temp); //从文件中读取 1k echo fread($temp,1024); //删除文件 fclose($temp); //文件会在关闭后(用 fclose())自动被删除,或当脚本结束后 //输出:Testing, www.phpernote.com
17、改变文件权限模式(chmod)
bool chmod ( file [,mode] )
mode 可选。规定新的权限。该参数由 4 个数字组成:
第一个数字永远是 0
第二个数字规定所有者的权限
第二个数字规定所有者所属的用户组的权限
第四个数字规定其他所有人的权限
可能的值(如需设置多个权限,请对下面的数字进行总计):
1 - 执行权限
2 - 写权限
4 - 读权限
<?php // 所有者可读写,其他人没有任何权限 chmod("test.txt",0600); // 所有者可读写,其他人可读 chmod("test.txt",0644); // 所有者有所有权限,其他所有人可读和执行 chmod("test.txt",0755); // 所有者有所有权限,所有者所在的组可读 chmod("test.txt",0740);
18、扩展函数,方法
php读取目录并列表显示目录中的文件的函数
PHP删除目录及目录下所有文件
更多文件,文件夹(目录)函数请参考:
PHP Filesystem 函数
您可能感兴趣的文章
- php清空(删除)指定目录下的文件,不删除目录文件夹的方法
- php判断文件或目录(文件夹)是否存在
- linux chmod(文件或文件夹权限设定)命令参数及用法详解
- MySQL通过命令形式导入与导出.sql文件备份数据操作的实例
- php提取身份证号码中的生日日期以及验证是否为未成年人的函数
- PHP向文件写入或追加数据
- linux删除文件,文件夹命令rm 命令详解
- Linux命令文件目录管理cat命令

What’s still popular is the ease of use, flexibility and a strong ecosystem. 1) Ease of use and simple syntax make it the first choice for beginners. 2) Closely integrated with web development, excellent interaction with HTTP requests and database. 3) The huge ecosystem provides a wealth of tools and libraries. 4) Active community and open source nature adapts them to new needs and technology trends.

PHP and Python are both high-level programming languages that are widely used in web development, data processing and automation tasks. 1.PHP is often used to build dynamic websites and content management systems, while Python is often used to build web frameworks and data science. 2.PHP uses echo to output content, Python uses print. 3. Both support object-oriented programming, but the syntax and keywords are different. 4. PHP supports weak type conversion, while Python is more stringent. 5. PHP performance optimization includes using OPcache and asynchronous programming, while Python uses cProfile and asynchronous programming.

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 originated in 1994 and was developed by RasmusLerdorf. It was originally used to track website visitors and gradually evolved into a server-side scripting language and was widely used in web development. Python was developed by Guidovan Rossum in the late 1980s and was first released in 1991. It emphasizes code readability and simplicity, and is suitable for scientific computing, data analysis and other fields.

PHP is suitable for web development and rapid prototyping, and Python is suitable for data science and machine learning. 1.PHP is used for dynamic web development, with simple syntax and suitable for rapid development. 2. Python has concise syntax, is suitable for multiple fields, and has a strong library ecosystem.

PHP remains important in the modernization process because it supports a large number of websites and applications and adapts to development needs through frameworks. 1.PHP7 improves performance and introduces new features. 2. Modern frameworks such as Laravel, Symfony and CodeIgniter simplify development and improve code quality. 3. Performance optimization and best practices further improve application efficiency.

PHPhassignificantlyimpactedwebdevelopmentandextendsbeyondit.1)ItpowersmajorplatformslikeWordPressandexcelsindatabaseinteractions.2)PHP'sadaptabilityallowsittoscaleforlargeapplicationsusingframeworkslikeLaravel.3)Beyondweb,PHPisusedincommand-linescrip

PHP type prompts to improve code quality and readability. 1) Scalar type tips: Since PHP7.0, basic data types are allowed to be specified in function parameters, such as int, float, etc. 2) Return type prompt: Ensure the consistency of the function return value type. 3) Union type prompt: Since PHP8.0, multiple types are allowed to be specified in function parameters or return values. 4) Nullable type prompt: Allows to include null values and handle functions that may return null values.


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

MantisBT
Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Dreamweaver Mac version
Visual web development tools

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

PhpStorm Mac version
The latest (2018.2.1) professional PHP integrated development tool

WebStorm Mac version
Useful JavaScript development tools