RecursiveDirectoryIterator目录操作类,recursive
RecursiveDirectoryIterator目录操作类,recursive
<span>/*</span><span>* * @author Funsion Wu * @abstract SPL使用案例,全国首发,技术分享,欢迎转帖 </span><span>*/</span> <span>class</span> <span>Dir</span> <span>extends</span><span> RecursiveDirectoryIterator { </span><span>const</span> CHILD_FIRST = RecursiveIteratorIterator::<span>CHILD_FIRST ; </span><span>const</span> LEAVES_ONLY = RecursiveIteratorIterator::<span>LEAVES_ONLY ; </span><span>const</span> SELF_FIRST = RecursiveIteratorIterator::<span>SELF_FIRST ; </span><span>/*</span><span> ideas:将Dir类设置为不变类,无状态类 </span><span>*/</span> <span>private</span> <span>static</span> <span>function</span> getDirIterator( <span>$dir</span>, <span>$mode</span>=self::<span>LEAVES_ONLY ) { </span><span>if</span>( !<span>file_exists</span>(<span>$dir</span>) ){ <span>exit</span><span> ; } </span><span>$dirIterator</span> = <span>new</span> RecursiveDirectoryIterator(<span>$dir</span><span>); </span><span>$objIterator</span> = <span>new</span> RecursiveIteratorIterator( <span>$dirIterator</span>, <span>$mode</span><span> ); </span><span>return</span> <span>$objIterator</span><span>; } </span><span>/*</span><span>* * 递归的删除目录 + ----------------------------------------------------- + * @param $dir 要删除的目录 * @param $delSelf 决定删除目录or清空目录,默认删除目录 </span><span>*/</span> <span>public</span> <span>static</span> <span>function</span> delDir( <span>$dir</span>, <span>$delSelf</span>=<span>true</span><span> ) { </span><span>$dirIterator</span> = self::getDirIterator(<span>$dir</span>, self::<span>CHILD_FIRST); </span><span>foreach</span> ( <span>$dirIterator</span> <span>as</span> <span>$file</span><span> ) { </span><span>if</span> ( <span>$file</span>-><span>isDir() ) { @ </span><span>rmdir</span>( <span>$file</span>-><span>getRealPath() ); }</span><span>else</span><span>{ @ </span><span>unlink</span>( <span>$file</span>-><span>getRealPath() ); } } </span><span>if</span>( <span>$delSelf</span> ) { @ <span>rmdir</span>(<span>$dir</span><span>); } } </span><span>/*</span><span>* * 递归的列出目录,遍历目录 + -------------------------- + * @param $dir 要操作的目录 </span><span>*/</span> <span>public</span> <span>static</span> <span>function</span> listDir ( <span>$dir</span><span> ) { </span><span>$dirIterator</span> = self::getDirIterator( <span>$dir</span>, self::<span>SELF_FIRST ); </span><span>foreach</span> ( <span>$dirIterator</span> <span>as</span> <span>$file</span><span> ) { </span><span>$filepath</span> = <span>str_replace</span>('\\' , '/' , <span>$file</span>-><span>getPath() ); </span><span>$deep</span> = <span>substr_count</span>( <span>$filepath</span> , '/'<span> ); </span><span>if</span>( <span>$file</span>-><span>isDir() ) { </span><span>$str</span> .= '<div>$deep</span> .'px"> + '<span> ; </span><span>$str</span> .= <span>$file</span>->getBasename() .'</div>'<span> ; }</span><span>elseif</span>( <span>$file</span>-><span>isFile() ){ </span><span>$str</span> .= '<div>$deep</span> .'px">' . <span>$file</span>->getBasename() .'</div>'<span>; } } </span><span>return</span> <span>$str</span><span> ; } </span><span>/*</span><span>* * 统计目录的信息(总字节数,文件数,目录数) + -----------------------------=----------- + * @param $dir 要操作的目录 * @return 由目录信息组成的数组 </span><span>*/</span> <span>public</span> <span>static</span> <span>function</span> countDir( <span>$dir</span><span> ) { </span><span>$countDir</span> = <span>$countFiles</span> = <span>$size</span> = 0<span> ; </span><span>$dirIterator</span> = self::getDirIterator( <span>$dir</span>, self::<span>SELF_FIRST ); </span><span>foreach</span> ( <span>$dirIterator</span> <span>as</span> <span>$file</span><span> ) { </span><span>if</span>( <span>$file</span>-><span>isDir() ) { </span><span>$countDir</span>++<span> ; }</span><span>elseif</span>( <span>$file</span>-><span>isFile() ){ </span><span>$countFiles</span>++<span> ; </span><span>$size</span> += <span>$file</span>-><span>getSize() ; } } </span><span>return</span> <span>array</span>( 'countDir'=><span>$countDir</span>, 'countFiles'=><span>$countFiles</span>, 'size'=><span>$size</span>.' Byte'<span> ); } </span><span>/*</span><span>* * 递归的创建目录 + -------------------- + * @param $dir 要创建的目录 * @param $mode 所创建目录的读写权限 </span><span>*/</span> <span>public</span> <span>static</span> <span>function</span> makeDir( <span>$dir</span>, <span>$mode</span>=0644<span> ) { </span><span>return</span> <span>mkdir</span>( <span>$dir</span>, <span>$mode</span>, <span>true</span><span> ); } } </span><span>/*</span><span> ========================== 调用方法 =========================== </span><span>*/</span> <span>//</span><span> Dir::delDir('./need_del_dir'); // echo Dir::listDir('tools'); // var_dump( Dir::countDir('tools') ); // Dir::makeDir( 'aaa/ccc/ddd/eee/fff' );</span>
Directory.CreateDirectory(@"C:\123");//创建123
File.Create(@"C:\123\456.txt").Close();//创建456.txt并关闭文件
File.Copy(@"C:\123\456.txt",@"D:\456.txt");//复制文件
注意创建了文件以后要Close()。因为此时这个文件被程序占用了,就不能进行复制。
filestream不具备复制文件的方法,它只负责文件内容的读写等功能
呵呵,当前目录就是你所在的位置,通俗点就是你在哪个文件夹里,你所在的文件夹或盘就是当前目录,必须是直接所在的目录才叫当前目录
如图的当前目录为“淘宝男装”

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

AI Hentai Generator
Generate AI Hentai for free.

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



PyCharm is a very popular Python integrated development environment (IDE). It provides a wealth of functions and tools to make Python development more efficient and convenient. This article will introduce you to the basic operation methods of PyCharm and provide specific code examples to help readers quickly get started and become proficient in operating the tool. 1. Download and install PyCharm First, we need to go to the PyCharm official website (https://www.jetbrains.com/pyc

LinuxDeploy operating steps and precautions LinuxDeploy is a powerful tool that can help users quickly deploy various Linux distributions on Android devices, allowing users to experience a complete Linux system on their mobile devices. This article will introduce the operating steps and precautions of LinuxDeploy in detail, and provide specific code examples to help readers better use this tool. Operation steps: Install LinuxDeploy: First, install

Presumably many users have several unused computers at home, and they have completely forgotten the power-on password because they have not been used for a long time, so they would like to know what to do if they forget the password? Then let’s take a look together. What to do if you forget to press F2 for win10 boot password? 1. Press the power button of the computer, and then press F2 when turning on the computer (different computer brands have different buttons to enter the BIOS). 2. In the bios interface, find the security option (the location may be different for different brands of computers). Usually in the settings menu at the top. 3. Then find the SupervisorPassword option and click it. 4. At this time, the user can see his password, and at the same time find the Enabled next to it and switch it to Dis.

With the popularity of smartphones, the screenshot function has become one of the essential skills for daily use of mobile phones. As one of Huawei's flagship mobile phones, Huawei Mate60Pro's screenshot function has naturally attracted much attention from users. Today, we will share the screenshot operation steps of Huawei Mate60Pro mobile phone, so that everyone can take screenshots more conveniently. First of all, Huawei Mate60Pro mobile phone provides a variety of screenshot methods, and you can choose the method that suits you according to your personal habits. The following is a detailed introduction to several commonly used interceptions:

The mobile version of WeChat Reading App is a very good reading software. This software provides a lot of books. You can read them anytime, anywhere with just one click to search and read them online. All of them are officially authorized and different types of books are neatly arranged. Sort and enjoy a comfortable and relaxing reading atmosphere. Switch the reading modes of different scenarios, update the latest book chapters continuously every day, support online login from multiple devices, and batch download to the bookshelf. You can read it with or without the Internet, so that everyone can discover more knowledge from it. Now the editor details it online Promote the method of viewing the catalog for WeChat reading partners. 1. Open the book you want to view the catalog and click in the middle of the book. 2. Click the three lines icon in the lower left corner. 3. In the pop-up window, view the book catalog

PHP String Operation: A Practical Method to Effectively Remove Spaces In PHP development, you often encounter situations where you need to remove spaces from a string. Removing spaces can make the string cleaner and facilitate subsequent data processing and display. This article will introduce several effective and practical methods for removing spaces, and attach specific code examples. Method 1: Use the PHP built-in function trim(). The PHP built-in function trim() can remove spaces at both ends of the string (including spaces, tabs, newlines, etc.). It is very convenient and easy to use.

Ele.me is a software that brings together a variety of different delicacies. You can choose and place an order online. The merchant will make it immediately after receiving the order. Users can bind WeChat through the software. If you want to know the specific operation method , remember to check out the PHP Chinese website. Instructions on how to bind WeChat to Ele.me: 1. First open the Ele.me software. After entering the homepage, we click [My] in the lower right corner; 2. Then in the My page, we need to click [Account] in the upper left corner; 3. Then come to the personal information page where we can bind mobile phones, WeChat, Alipay, and Taobao. Here we click [WeChat]; 4. After the final click, select the WeChat account that needs to be bound in the WeChat authorization page and click Just [Allow];

tree is a command line tool that recursively lists the contents of a directory in a tree format, so that all directories, subdirectories, and files are listed in a hierarchical manner, thereby visually displaying the organizational structure of files and folders. The following are the installation and use methods of tree under Windows and Linux systems. The installation and use of tree under Linux. Installing tree under Linux: aptupdate&&aptinstalltree The following are the common ways of using the tree command. #Display the directory tree under the specified path tree/d/temp #Limit the maximum display depth tree-L3 #Display only directories but not files tree-d #Display including hidden files and directories tr
