PHP directory and file information in an array -
so there user directory space. each user gets own space , can upload .gif, jpg, png, , mp4 files them.
the user can create , manage own files in many directories or sub-directories wish.
i have no idea how need end result have no example code. need end result follows:
foreach ($file $file) { $data[] = array('type' => 'image', 'path' => $file["path"], 'name' => $file[name], 'time' => $file["update_time"]); }
how details directories keeping in mind have no idea structure after base directory.
also, need exclude directories , files begin . (such .htaccess, there others)
here go. usa recursive function iterate on each directory. if directory recursion, otherwise , store data:
$directorytoscan = './'; $results = []; $tree = getdirenties($directorytoscan, $results); var_dump($tree); function getdirenties($directory, &$results) { $entries = scandir($directory); foreach ($entries $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; }
Comments
Post a Comment