首頁 >php教程 >php手册 >PHP面试题之文件目录操作,

PHP面试题之文件目录操作,

WBOY
WBOY原創
2016-06-13 08:53:131326瀏覽

PHP面试题之文件目录操作,

获取文件后缀,遍历目录层次

<span>/*</span><span>*
* 5种方式获取文件后缀名
* 这道题其实就是考函数substr() strrchr()  array_pop()  strrpos() strpos() strrev() explode()  pathinfo()
* @param string $filename 要处理的文件名  1.jpg/1.png/1.html/1.php
* @return string .php/php  
</span><span>*/</span>
<span>function</span> get_ext1(<span>$filename</span><span>)
{
        </span><span>return</span> <span>strrchr</span>(<span>$filename</span>,'.'<span>);
}

</span><span>function</span> get_ext2(<span>$filename</span><span>){
        </span><span>return</span> <span>substr</span>(<span>$filename</span>,<span>strrpos</span>(<span>$filename</span>,'.'<span>));
}

</span><span>function</span> get_ext3(<span>$filename</span><span>){
        </span><span>return</span> <span>array_pop</span>(<span>explode</span>('.',<span>$filename</span><span>));
}

</span><span>function</span> get_ext4(<span>$filename</span><span>){
        </span><span>return</span> <span>pathinfo</span>(<span>$filename</span>,<span>PATHINFO_EXTENSION);
}

</span><span>function</span> get_ext5(<span>$filename</span><span>){
        </span><span>return</span> <span>strrev</span>(<span>substr</span>(<span>strrev</span>(<span>$filename</span>),0,<span>strpos</span>(<span>strrev</span>(<span>$filename</span>),'.'<span>)));
}

</span><span>$filename</span> = "1.php"<span>;
</span><span>echo</span> get_ext1(<span>$filename</span>)."\n"<span>;
</span><span>echo</span> get_ext2(<span>$filename</span>)."\n"<span>;
</span><span>echo</span> get_ext3(<span>$filename</span>)."\n"<span>;
</span><span>echo</span> get_ext4(<span>$filename</span>)."\n"<span>;
</span><span>echo</span> get_ext5(<span>$filename</span>)."\n";

 

<span>/*</span><span>*
* 遍历目录(递归)
* @param string $dir 目录名且最后没有/
* @return array $files 整个目录结构
</span><span>*/</span>
<span>function</span> myScandir(<span>$dir</span><span>)
{
        </span><span>$files</span> = <span>array</span><span>();
        </span><span>if</span>(<span>$handle</span> = <span>opendir</span>(<span>$dir</span><span>)){
                </span><span>while</span> ((<span>$file</span> = <span>readdir</span>(<span>$handle</span>)) != <span>false</span><span>){
                        </span><span>if</span>(<span>$file</span> != "." && <span>$file</span> != ".."<span>){
                                </span><span>$newDir</span> = <span>$dir</span>."/".<span>$file</span><span>;
                                </span><span>if</span>(<span>is_dir</span>(<span>$newDir</span><span>)){
                                        </span><span>$files</span>[<span>$file</span>] = myScandir(<span>$newDir</span><span>);
                                }</span><span>else</span><span>{
                                        </span><span>$files</span>[] = <span>$file</span><span>;
                                }
                        }
                }
                </span><span>closedir</span>(<span>$handle</span><span>);
                </span><span>return</span> <span>$files</span><span>;
        }
}

</span><span>$dir</span> = "/dirname"<span>;
</span><span>print_r</span>(myScandir(<span>$dir</span>));

 

陳述:
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn