2

As I only get one very very tiny part of the PHP phenomenon I'm very happy that I managed to create the following script by searching the web. As you can see, this shows the files present in the folder 'archive' on the website I'm building. It's for the newspaper of my school. The old papers will be uploaded to this folder. A problem is that the order is very messy at the moment. I know PHP can sort things with sort(), but I'm not sure how to do that in this case. Could anyone explain how to get it fixed?

Another thing is hiding the extensions. I couldn’t find it yet, is there a possibility for that?

You’d be a great help if you could help me at least with the first thing :)

<?php
    if ($handle = opendir(archive)) {
        $ignore_files = array('.', '..', '.htaccess', '.htpasswd', 'index.php');
        while (false !== ($file = readdir($handle)))
        {
            if (!in_array($file, $ignore_files))
            {
                $thelist .= '<a href="/archive/'.$file.'">'.$file.'</a>'.'<br>';
            }
        }
        closedir($handle);
    }
?>
<p><?=$thelist?></p>
Nicolás Ozimica
  • 9,481
  • 5
  • 38
  • 51
Stijn
  • 103
  • 1
  • 4
  • 14
  • To hide or to get the extension of a file, this discussion will be useful to you: http://stackoverflow.com/questions/1351543/get-the-file-extension-basename – Nicolás Ozimica Nov 14 '11 at 16:45

1 Answers1

1

If you want to get the list of files from a directory, (and then sort that list), also stripping off the extension, you would do this:

<?php
// $archive variabe assumed to point to '/archive' folder
$ignore_files = array('.', '..', '.htaccess', '.htpasswd', 'index.php');

// into array $files1 will be every file inside $archive:
$files1 = scandir($archive);
sort($files1);

foreach ($files1 as $file) {
    if (!in_array($file, $ignore_files)) {
        $fileParts = pathinfo($file);
        $thelist .= '<a href="/archive/'.$file.'">'.$fileParts['filename'].'</a>'.'<br>';
    }
}
?>
<p><?=$thelist?></p>
Nicolás Ozimica
  • 9,481
  • 5
  • 38
  • 51
  • Thanks a lot for your reply! Unfortunately I couldn't figure out what part of the code this is (I suppose this is not the full code as I see some parts are missing and it doesn't work on its own). Could you help me putting it all together again please? – Stijn Nov 14 '11 at 17:36
  • @Stijn: I missed the definition of the ignored files array. I've tested my corrected code and works. – Nicolás Ozimica Nov 14 '11 at 17:47
  • @Nicholás: And I missed you'd created a variable '$archive'. It didn't point to anything yet, so that was the biggest problem. Thanks for adding the ignored file line again. Now it works perfect! – Stijn Nov 14 '11 at 18:50