php 算法 文件及url 处理

(1)遍历文件夹下的所有文件夹 

<?php    
    function my_scandir($dir)
    {
        $files = array();
        if($handle = opendir($dir))
        {
            while (($file = readdir($handle))!== false) 
            {
                if($file != '..' && $file != '.')
                {
                    if(is_dir($dir."/".$file))
                    {
                        $files[$file]=my_scandir($dir."/".$file);
                    }
                    else
                    {
                        $files[] = $file;
                    }
                }
            }

            closedir($handle);
            return $files;
        }
    }

    var_dump(my_scandir('../'));
?>
View Code

(2)从一个标准url中取出文件的扩展名

<?php
    function getExt($url)
    {
        $arr = parse_url($url);//parse_url解析一个 URL 并返回一个关联数组,包含在 URL 中出现的各种组成部分
        //'scheme' => string 'http' (length=4)
        //'host' => string 'www.sina.com.cn' (length=15)
        //'path' => string '/abc/de/fg.php' (length=14)
        //'query' => string 'id=1' (length=4)
        $file = basename($arr['path']);// basename函数返回路径中的文件名部分
        $ext = explode('.', $file);
        return $ext[count($ext)-1];
    }

    print(getExt('http://www.sina.com.cn/abc/de/fg.html.php?id=1'));

?>
View Code
原文地址:https://www.cnblogs.com/paopao123/p/11131362.html