PHP_常见面试题(1)

1)扫描一个文件夹下的所有文件夹和文件:

PHP_常见面试题(1)
<?php

function scan($path){

    static $arr=array();    //静态数组,用于存储扫描到的和文件夹

    $filearr=scandir($path);

    foreach ($filearr as $value){

        if($value=='.' || $value=='..'){

            continue;

        }

        if(is_file($path.'/'.$value)){

            $arr[]='<font color=green>'.$path.'/'.$value.'</font>';

        }

        else {

            $arr[]='<font color=red>'.$path.'/'.$value.'</font>';

            scan($path.'/'.$value);

        }

        //            if(is_dir($path.'/'.$value)){

        //                scan($path.'/'.$value);

        //            }

    }

    return $arr;

}

$row=scan('../../');

$count=count($row);

echo '共查到'.$count.'个文件!<br/>';

foreach ($row as $value)

echo $value.'<br/>';
View Code

2)删除非空文件夹:

PHP_常见面试题(1)
<?php

function del($path) {

    $res = @opendir ( $path );

    if (! $res) {

        echo '文件或文件夹不存在,删除失败!';

        exit ();

    }

    while ( $file = readdir ( $res ) ) {

        if ($file != "." && $file != "..") {

            $fullpath = $path . "/" . $file;

            if (! is_dir ( $fullpath )) {

                unlink ( $fullpath );

            } else {

                del ( $fullpath );

            }

        }

    }

    closedir ( $res );

    if (rmdir ( $path )) {

        return true;

    } else {

        return false;

    }

}



$path = "./a/";

if (del ( $path )) {

    echo "删除成功!";

} else {

    echo "删除失败!";

}



?>
View Code

 

你可能感兴趣的:(PHP)