1

So there is user directory space. Each user gets their own space and they can upload .gif, jpg, png, and mp4 files to them.

The user can create and manage their own files in as many directories or sub-directories as they wish.

I have no idea how to do what I need to for my end result so I have no example code. What I need for an end result is as follows:

foreach ($file AS $file) {
    $data[] = array('type' => 'image', 
    'path' => $file["path"],
    'name' => $file[name], 
    'time' => $file["update_time"]);
}

How exactly would I get the details from the directories keeping in mind I have no idea what the structure will look like after the base directory.

Also, I need to exclude all directories and files that begin with a . (such as .htaccess, but there are others)

3
  • 1
    You iterate in a recursive manner over the entries you get from a call to glob(). That also allows filtering, for example to ignore files whose name start with a dot (.). Commented Sep 22, 2016 at 11:30
  • You need to create a recursive function what is go thorughs on all entry. Your friends will be scandir, is_dir, in_array Commented Sep 22, 2016 at 11:31
  • glob looks like a LOVELY way to go. The fact someone nearly has the exact code I need on the php page makes it even better. Commented Sep 22, 2016 at 11:39

1 Answer 1

2

Here you go. Usa recursive function to iterate on each directory. If it is a directory do the recursion, otherwise get and store data:

$directoryToScan = './';
$results = [];
$tree = getDirEnties($directoryToScan, $results);
var_dump($tree);

function getDirEnties($directory, &$results) {
    $entries = scandir($directory);
    foreach ($entries as $item) {
        if (!in_array($item, ['.', '..']) && substr($item, 0, 1) !== '.') {
            $path = $directory . '/' . $item;
            if (is_dir($path)) {
                getDirEnties($path, $results);
            } else {
                $pathInfo = pathinfo($path);
                $name = $pathInfo['filename'];
                $type = 'unknown';
                if (!empty($pathInfo['extension'])) {
                    $name .= "." . $pathInfo['extension'];
                    switch (strtolower($pathInfo['extension'])) {
                        case "gif":
                        case "jpg":
                        case "png":
                        case "jpeg":
                        case "bmp":
                            //etc..
                            $type = 'image';
                            break;
                        case "mp4":
                            $type = 'media';
                            break;
                    }
                }
                $data = [
                    'name' => $name,
                    'path' => $pathInfo['dirname'],
                    'type' => $type,
                    'time' => filemtime($path)
                ];
                $results[] = $data;
            }
        }
    }
    return $results;
}
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.