title: How to display images from a folder using php - PHP
body: <p>It would be great if someone could help me figure out why the browser cannot load the images (error 404). The code works, and the image source is correct, but I cannot figure out what is wrong. (using localhost) </p>
  
  <pre><code>$dir          = '/home/user/Pictures';
  $file_display = array(
      'jpg',
      'jpeg',
      'png',
      'gif'
  );
  
  if (file_exists($dir) == false) {
      echo 'Directory \'', $dir, '\' not found!';
  } else {
      $dir_contents = scandir($dir);
  
      foreach ($dir_contents as $file) {
          $file_type = strtolower(end(explode('.', $file)));
  
          if ($file !== '.' &amp;&amp; $file !== '..' &amp;&amp; in_array($file_type, $file_display) == true) {
              echo '&lt;img src="', $dir, '/', $file, '" alt="', $file, '" /&gt;';
          }
      }
  }
  </code></pre>