PHP遍歷文件夾及其文件

公司品牌策劃部針對2018年母親節作了一個活動,活動統計涉及到了一個展現全部做品的頁面。php

因開發整個程序中沒有涉及到數據庫讀寫操做,因此這裏的展現就只能經過遍歷文件夾的方式來實現了。數據庫

PHP 遍歷文件夾下全部文件

$path = '/tmp/cus_img/';
if ($hd = opendir('.' . $path)) {
    while (false !== ($file = readdir($hd))) {
        if ($file != "." && $file != "..") {
            echo $path . $file;
        }
    }
    closedir($handle);
}

PHP 遍歷文件夾及其子目錄下全部文件

$path = '/tmp/cus_img';
$files = get_files($path);

function get_files($path) {
    $files_arr = null;
    if( is_dir($path) ){
        if( $handle = opendir($path) ){
            while( ($file=readdir($handle)) !== false) {
                if( $file != '.' && $file != ".." ) {
                    if( is_dir($path."/".$file) ){
                        $files_arr[$file] = get_files($path . "/" . $file);
                    } else {
                        $files_arr[] = $path . "/" . $file;
                    }
                }
            }
            closedir($handle);  
        }
    }
    return $files_arr;  
}

PHP 刪除目錄下全部文件

function dir_clear($dir) {
    $directory = dir($dir);
    while($entry = $directory->read()) {
        $filename = $dir.'/'.$entry;
        if(is_file($filename)) {
            @unlink($filename);
        }
    }
    $directory->close();
    result();
}
相關文章
相關標籤/搜索