List files from a directory containing name, size and last modified PHP files

Asked

Viewed 81 times

0

I need to make a web application that scans multiple directories, take the "last modified" and use the time to perform a SLA calculation to alert on the user screen.

I took a look at some code in PHP to scan directory and managed to do this below.

<?php
  if ($dir[strlen($dir)-1] != '/') $dir .= '/';

  if (!is_dir($dir)) return array();

  $dir_objects = array();
  while ($object = readdir($dir_handle)) {
    if (!in_array($object, array('.','..'))) {
      $filename    = $dir.$object;
      $file_object = array(
          'name' => $object,
          'size' => filesize($filename),
          'type' => filetype($filename),
          'time' => date("d/m/Y H:i:s", filemtime($filename))
      );
      $dir_objects[] = $file_object;
    }
  }
  print_r($dir_objects);
?>

Note: I have a slight experience with PHP, so I’m asking for help. ^^'

  • Specify if the server is Windows or Linux, you can also check the PHP website for the stat function: https://php.net/manual/en/function.stat.php

  • 1

    Thanks for the help João!

1 answer

0


I got it working!

  $path = "/media/Development/robotQuerys";
  $files = scandir($path);
  //var_dump($path);
  //var_dump($files);
  $dirFiles = array();
  foreach($files as $file){
    $filename = $path.DIRECTORY_SEPARATOR.$file;
    if(!is_dir($filename)){
      $info = pathinfo($filename);
      $info["size"] = filesize($filename);
      $info["modified"] = date("d/m/Y H:i:s", filemtime($filename));
      //$info["url"] = "http://".$filename;

      array_push($dirFiles, $info);
    }
  }
  echo "<br/>";
  //var_dump($dirFiles);
  foreach($dirFiles as $file){
    echo "Name: ".$file["basename"]." | Size: ".$file["size"]."KByte | Modified: ".$file["modified"]."<br/>";

Thanks for the help. ;)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.