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

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

WBOY
Freigeben: 2016-06-13 08:53:13
Original
1318 Leute haben es durchsucht

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";
Nach dem Login kopieren

<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>));
Nach dem Login kopieren

 

Quelle:php.cn
Erklärung dieser Website
Der Inhalt dieses Artikels wird freiwillig von Internetnutzern beigesteuert und das Urheberrecht liegt beim ursprünglichen Autor. Diese Website übernimmt keine entsprechende rechtliche Verantwortung. Wenn Sie Inhalte finden, bei denen der Verdacht eines Plagiats oder einer Rechtsverletzung besteht, wenden Sie sich bitte an admin@php.cn
Beliebte Empfehlungen
Beliebte Tutorials
Mehr>
Neueste Downloads
Mehr>
Web-Effekte
Quellcode der Website
Website-Materialien
Frontend-Vorlage