简体   繁体   中英

linking directories and files in php

Hello stackoverflow community;

I'm trying to display a few files in a directory using php and coming unstuck:

In my file ('salad') I have three recipe files ('recipe1.txt', recipe2.txt, 'recipe3.txt') and I want to display them so I'm writing the following:

        $script = opendir('salad');
          while(false !==($file = readdir($script))) {
             if (is_file($file)) {
               echo "<p>$file</p>";
                 }
             }

Unfortunately this only echos to the screen .DS_store, what am i doing wrong?

You could use this:

<?php
$dir = "/tmp"; // put what suits you here
$dh  = opendir($dir);
while (false !== ($filename = readdir($dh))) {
    $files[] = $filename;
}

sort($files);

print_r($files);

rsort($files);

echo"$files";

?>

source: http://php.net/manual/en/function.scandir.php

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