RecursiveDirectoryIterator目录操作类,recursive_PHP教程

WBOY
發布: 2016-07-13 10:17:48
原創
1298 人瀏覽過

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
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板
關於我們 免責聲明 Sitemap
PHP中文網:公益線上PHP培訓,幫助PHP學習者快速成長!