简体   繁体   中英

Listing out files in PHP (ONLY the files) as links from all subdirectories of a directory

I have a ftp site that hosts several files. Each file is in its own folder by itself in the main directory (uploads), due to restrictions in place by the downloading software we are using (each file must have a unique filepath, and the file name has to be modpack.zip (cannot be any variation of this name, has to have that exact name, hence why each version is in its own subfolder)

I need to find a way to list all the files from all subdirectories of the main upload folder in a table, as well as list how big each file is. Each file must have a link to it, and the folders do not need to be listed.

The code I have so far as my starting point is:

<div id="list"><table>
<tr><td align="center">Modpack List</td></tr>
<?php

$dir_open = opendir('uploads/');

while(false !== ($filename = readdir($dir_open))){
    $bytes = filesize($_SERVER['DOCUMENT_ROOT']."/uploads/".$filename."");
    if($filename != "." && $filename != ".."){
        echo '<tr><td><a href="/uploads/'.$filename.'">'.$filename.'</a> - '.bytesToSize($bytes).'</td></tr>';
    }
}

closedir($dir_open);

?>
</table>
</div>

I have tried using readdir() and scandir(), but both list both the files and subfolders, which I don't want, and the current setup only list any files and folders directly under /uploads, and won't list any of the files under the subfolders unless I click the folder, which takes me to an FTP page for that folder.

I have tried several posted solutions I saw on here, as well as on other sites, but none of which did what I need, and I can't figure out how to make it omit the folders as links, and only list the files.

Try using glob():

<?php
    foreach(glob('path-to/UPLOAD_FOLDER/*') as $subDirectory) {         
        foreach(glob($subDirectory'./*') as $file) {

            // do what you want with file here... example:
            $file_list .= $file.'<br>';                             
        }                               
    }
    echo $file_list;
?>  

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM