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

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

WBOY
WBOYOriginal
2016-06-13 08:53:131318browse

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>));

 

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn