首頁 > 後端開發 > php教程 > PHP面试题之文件目录操作,_PHP教程

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

WBOY
發布: 2016-07-12 09:07:28
原創
1090 人瀏覽過

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>));
登入後複製

 

www.bkjia.comtruehttp://www.bkjia.com/PHPjc/1060690.htmlTechArticlePHP面试题之文件目录操作, 获取文件后缀,遍历目录层次 /* ** 5种方式获取文件后缀名* 这道题其实就是考函数substr() strrchr() array_pop() strrp...
來源:php.cn
本網站聲明
本文內容由網友自願投稿,版權歸原作者所有。本站不承擔相應的法律責任。如發現涉嫌抄襲或侵權的內容,請聯絡admin@php.cn
熱門教學
更多>
最新下載
更多>
網站特效
網站源碼
網站素材
前端模板