> 백엔드 개발 > PHP 튜토리얼 > RecursiveDirectoryIterator目录操作类,recursive_PHP教程

RecursiveDirectoryIterator目录操作类,recursive_PHP教程

WBOY
풀어 주다: 2016-07-13 10:17:48
원래의
1332명이 탐색했습니다.

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>
로그인 후 복사

 

C#目录操作

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不具备复制文件的方法,它只负责文件内容的读写等功能
 

操作系统中什是当前目录?起作用是什

呵呵,当前目录就是你所在的位置,通俗点就是你在哪个文件夹里,你所在的文件夹或盘就是当前目录,必须是直接所在的目录才叫当前目录
如图的当前目录为“淘宝男装”


RecursiveDirectoryIterator目录操作类,recursive_PHP教程
 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/888102.htmlTechArticleRecursiveDirectoryIterator目录操作类,recursive /* * * @author Funsion Wu * @abstract SPL使用案例,全国首发,技术分享,欢迎转帖 */ class Dir extends Recurs...
관련 라벨:
원천:php.cn
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿