Hướng dẫn php scandir recursive

You can scan directory recursively in this way the target is your top most directory:

function scanDir[$target] {

        if[is_dir[$target]]{

            $files = glob[ $target . '*', GLOB_MARK ]; //GLOB_MARK adds a slash to directories returned

            foreach[ $files as $file ]
            {
                scanDir[ $file ];
            }


        } 
    }

You can adapt this function for your need easily. For example if would use this to delete the directory and its content you could do:

function delete_files[$target] {

        if[is_dir[$target]]{

            $files = glob[ $target . '*', GLOB_MARK ]; //GLOB_MARK adds a slash to directories returned

            foreach[ $files as $file ]
            {
                delete_files[ $file ];
            }

            rmdir[ $target ];

        } elseif[is_file[$target]] {

            unlink[ $target ];
    }

You can not do this in the way you are doing. The following function gets recursively all the directories, sub directories so deep as you want and the content of them:

function assetsMap[$source_dir, $directory_depth = 0, $hidden = FALSE]
    {
        if [$fp = @opendir[$source_dir]]
        {
            $filedata   = array[];
            $new_depth  = $directory_depth - 1;
            $source_dir = rtrim[$source_dir, '/'].'/';

            while [FALSE !== [$file = readdir[$fp]]]
            {
                // Remove '.', '..', and hidden files [optional]
                if [ ! trim[$file, '.'] OR [$hidden == FALSE && $file[0] == '.']]
                {
                    continue;
                }

                if [[$directory_depth < 1 OR $new_depth > 0] && @is_dir[$source_dir.$file]]
                {
                    $filedata[$file] = assetsMap[$source_dir.$file.'/', $new_depth, $hidden];
                }
                else
                {
                    $filedata[] = $file;
                }
            }

            closedir[$fp];
            return $filedata;
        }
        echo 'can not open dir';
        return FALSE;
    }

Pass your path to the function:

$path = 'elements/images/';
$filedata = assetsMap[$path, $directory_depth = 5, $hidden = FALSE];

$filedata is than an array with all founded directories and sub directories with their content. This function lets you scan the directories structure [$directory_depth] so deep as you want as well get rid of all the boring hidden files [e.g. '.','..']

All you have to do now is to use the returned array, which is the complete tree structure, to arrange the data in your view as you like.

What you are trying to do is in fact a kind of file manager and as you know there are a lot of those in the wild, open source and free.

I hope this will help you and I wish you a merry Christmas.

Hướng dẫn dùng over load trong PHP

Tải chồng trong PHP mang nghĩa tạo các thuộc tính và phương thức động. Những thành phần động này được xử lý thông qua các magic method được thiết lập ...

Hướng dẫn php kết nối mysql

Th6 09, 2022 Hai G. 6ít nhất Đọc Nếu bạn là người mới trong giai đoạn lập trình viên, việc biết cách kết nối PHP với MySQL database rất hữu ích. Bạn ...

Hướng dẫn dùng mail to trong PHP

Nội dung1. Hàm mail PHP là gì?2. Các tùy chọn gửi mail PHPCú pháp mail [Email Syntax]Tham số Email [Email Parameters]To:Subject:Message:Headers:Parameters:3. Lý do nên dùng mail ...

Hướng dẫn dùng string tags trong PHP

Hàm strip_tags[] sẽ loại bỏ các thẻ HTML và PHP ra khỏi chuỗi. Hàm sẽ trả về chuỗi đã loại bỏ hết các thẻ HTML và PHP.Nội dung chínhCùng chuyên ...

Hướng dẫn get milliseconds php

Hướng dẫn php iterablesLặp lại trong PHP hướng đối tượng – PHP Iterables Lặp lại ta có thể coi là một kiểu giả trong PHP. Nó chấp nhận bất kỳ mảng hoặc ...

Hướng dẫn dùng emptu trong PHP

Định Nghĩa.Cú Pháp.Những sự khác biệt của hàm ở từng phiên bản.Ví dụ:Định Nghĩa.– Hàm empty[] là hàm dùng để kiểm tra giá trị biến có rỗng không.Cú ...

Hướng dẫn dùng 12.0.2 monterey trong PHP

RepliesPHP has been removed in macOS Monterey.Nội dung chínhHow do you check if I have PHP installed on Mac?Is PHP installed on Mac by default?How do I update PHP on my Mac?How do I reinstall PHP ...

Php translate string to english

been googleing for a while how is the best way to translate with google translator in PHP, found very different ways converting URLS, or using Js but i want to do it only with php [or with a very ...

Hướng dẫn dùng timezone offsets trong PHP

Whats the easiest way to get the UTC offset in PHP, relative to the current [system] timezone?Nội dung chính Not the answer youre looking for? Browse other questions tagged php timezone utc or ...

Hướng dẫn php dns cache

Hầu hết các web server có thể xử lý các lượng truy cập bình thường và đa phần các trang web không có quá nhiều truy cập. Vì vậy, có thể bạn tự hỏi: Tại ...

Hướng dẫn dùng addfile php trong PHP

Làm sao để thực hiện nén file .zip bằng PHP, bài viết này sẽ giới thiệu và hướng dẫn các bạn thực hiện nén file .zip bằng PHP một cách đơn giản. Mục ...

Hướng dẫn dùng echo result trong PHP

Hàm echo[] trong php giúp chúng ta xuất dữ liệu một cách dễ dàng. Cú phápecho [$var_1[, $var_k]] hoặc echo $var_1[, $var_k]Input: Danh sách các biến cần xuất dữ liệu ...

Can i convert python code to php?

Is there a software converter out there that can automatically convert this python code to PHP?#!/usr/bin/python import math def calcNumEntropyBits[s]: if len[s]

Chủ Đề