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

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

WBOY
Lepaskan: 2016-06-13 08:53:13
asal
1321 orang telah melayarinya

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";
Salin selepas log masuk

<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>));
Salin selepas log masuk

 

sumber:php.cn
Kenyataan Laman Web ini
Kandungan artikel ini disumbangkan secara sukarela oleh netizen, dan hak cipta adalah milik pengarang asal. Laman web ini tidak memikul tanggungjawab undang-undang yang sepadan. Jika anda menemui sebarang kandungan yang disyaki plagiarisme atau pelanggaran, sila hubungi admin@php.cn
Cadangan popular
Tutorial Popular
Lagi>
Muat turun terkini
Lagi>
kesan web
Kod sumber laman web
Bahan laman web
Templat hujung hadapan