Formatting strings of print_r($_FILES);

I am using print_r($_FILES); to show the list of JPG image files in a server. Is there any way to take that output and format to show the images as thumbnails with the filename to the right of them? I don’t know how to capture the stream in a useful format to convert it.

You can use scandir() to get a list of file names (be sure to check for . and … which denote directories and also check for child directories using is_dir(). Example code in the function page). When you get an array of file names, you can output the url to the image in an <img> element with the image scaled to your liking using a loop. Assuming this is for administrative use. If you are going to do this for visitors, you may want to consider creating thumbnails.

Thank you! I’ll look up scandir() to understand further. I expect to create thumbnails.

I went to http://php.net/manual/en/function.scandir.php and pulled an example from the page and used the following code. The thumbnails are not showing up for the jpgs. Clicking on the filenames do not show the image, but text. I’m clueless as to how to adapt it. I replaced many of the “dir” for the actual folder name, but that still doesn’t help. Any clues you can see as to what’s wrong? (In several places I put “enter your domain here” to show where I replaced “dir” with my folder.)

<?php

//-- optional placement
$exclude_list = array(".", "..", ".htaccess", "php.ini");

// ENTER YOUR DOMAIN HERE
if (isset($_GET["uploads"])) {
// ENTER YOUR DOMAIN HERE
  $dir_path = $_SERVER["DOCUMENT_ROOT"] . "/" . $_GET["uploads/"];
}
else {
  $dir_path = $_SERVER["DOCUMENT_ROOT"] . "/";
}

function dir_nav() {
  global $exclude_list, $dir_path;
  $directories = array_diff(scandir($dir_path), $exclude_list);
  //-- separator
  foreach($directories as $entry) {
    if(is_file($dir_path.$entry)) {
      echo "<p><a href='?file=" . $_GET["dir"] . $entry . "'><img style='width=300;' src='" . $_GET["dir"] . $entry . "'>" . $entry . "</a></p>";
    }
  }
}
dir_nav();

if (isset($_GET["file"])) {
  echo "<div style='margin:1em;border:1px solid gray;'>";
  highlight_file($dir_path.$_GET['file']);
  echo "</div>";
}
?>

</body>
</html> 

I am using an alternate method. This page already has everything on it:
https://raw.githubusercontent.com/firedev/indexr/master/index.php

Just drop it into the page as index.php and it will format all the images for you!

from: https://github.com/firedev/indexr

If you got what you want then I suppose there is nothing more to add. :slight_smile: Looks like a nice script you found.