0

I've finally found a way to get all the files from a folder to display on the site but for some reason it always displays 3 Div's that are empty and i really mean empty not some dots like in this post:

php scandir produces extra elements (2 dots)

Even without having any files in the folder. As seen on the picture

enter image description here

Does anyone have an idea why it behaves this way? I've included the code snippet that is causing the issue i think.

<div class="margin">
    <div class="left js-equal-height" >
        <h2>Recent nieuws</h2>
                <?php
    $dir = 'Nieuws';
    $file_display = ['txt'];
    $value = 0;
    if (file_exists($dir) == false) {
        return ["Directory \'', $dir, '\' not found!"];
    } else {
        $dir_contents = scandir($dir);
        foreach ($dir_contents as $file) {

        $img = basename($file, ".txt") . ".png";
        $src = 'Nieuws/pics/'.$img;

        echo '<div class="frontNieuws">';


        if (@getimagesize($src)) {
            echo '<img class="nieuwsFoto" 
              src="Nieuws\pics\\' . $img . '" alt="Nieuwsfoto">';
        } else {
            echo '<img class="nieuwsFoto" 
              src="Nieuws\pics\default.png" alt="Nieuwsfoto">';
        }
        echo file_get_contents($dir . '/'. $file, 
                      false, null, 0, 200) . " ... Lees meer!";
        echo '</div>';
        }
    }

  ?>

Community
  • 1
  • 1
DjKillerMemeStar
  • 425
  • 5
  • 18
  • Possible duplicate of [php scandir produces extra elements (2 dots)](http://stackoverflow.com/questions/36907031/php-scandir-produces-extra-elements-2-dots) – Umur Karagöz Jan 29 '17 at 19:35
  • I've looked at the solutions stated on that post but that did not seem to help. Also if you would have taken the time to look at the post of mine and looked at the picture you would have seen that its empty. The dots that you can see on the picture are hard coded in. – DjKillerMemeStar Jan 29 '17 at 19:49
  • Hmm... Let us make sure then. Could you please try this `$dir_contents = array_diff(scandir($dir), ['.', '..']);` and tell me whether it makes a difference or not? – Umur Karagöz Jan 29 '17 at 20:08
  • This kinda works as now it only displays one extra element instead of 2. How would i get rid of the third one? I tryed adding '...' but that didnt seel to change anything. – DjKillerMemeStar Jan 31 '17 at 08:56
  • That would help if you put `var_dump($dir_contents); die;` on the next line and post back what is being dumped (printed) so I can see it. Because it must work as expected now. Dumping two extra elements is a common mistake with `scandir()`. But I haven't heard 3 extra elements so far. – Umur Karagöz Jan 31 '17 at 09:03
  • `array(2) { [2]=> string(11) "frat"a'.txt" [3]=> string(4) "pics" }` This is what it returns. And accordng to this i think it also takes te folder thats in there as a file is this true? – DjKillerMemeStar Jan 31 '17 at 09:10
  • Yes, you can [take a look at here](http://stackoverflow.com/a/36907868/4306828) for more info on that. Problems seems to be that .txt file you have. It seems there is a `"` in your filename which should not be there. Can you delete or move that file and try again to see what happens? You can put a normal .txt file to test it too. – Umur Karagöz Jan 31 '17 at 09:18
  • Oke after deleting the file's that where causing the issue it still scans the folder and i can't seem to figure out why. Can you give me and example that works for my code please? – DjKillerMemeStar Jan 31 '17 at 09:56

0 Answers0