john pfeiffer
  • Home
  • Categories
  • Tags
  • Archives

php filetime multi column sort self submit

<?php       // 2011-06 johnpfeiffer sort by filename or last modified timestamp with self submit

function displayInfoAsTableRow( $filename , $filelastmodifiedtimestamp )
{
    if( !is_null( $filename ) && !is_null( $filelastmodifiedtimestamp ) )
    {
        print "<tr>";
        print "<td>" . $filename . "</td>";
        print "<td>" . $filelastmodifiedtimestamp . "</td>";    //debugging
        print "<td>" . date( "Y-m-d H:s T", $filelastmodifiedtimestamp ) . "</td>";
        print "</tr>\n";
    }
} //end displayFileInformation()


function displayListing( $filelist )
{
    print "<table>\n";
    foreach( $filelist as $key => $value )
    {
        displayInfoAsTableRow( $filelist[ $key ]['name'] , $filelist[ $key ]['lastmodifiedtimestamp'] );
    }
    print "</table>";

} // end displayListing()




    date_default_timezone_set('UTC');
  clearstatcache();
    $currentdirectory = getcwd();
    $currentdirectoryhandle = opendir( $currentdirectory );

    if( $currentdirectoryhandle )
    {
    $name = readdir( $currentdirectoryhandle );

    while( $name !== false )
    {
      if( $name != "." && $name != ".." && is_file($name) )
      {
        $lastmodifiedtimestamp = filemtime( $name );
        $filelist[] = array( "name" => $name, "lastmodifiedtimestamp" => $lastmodifiedtimestamp );
      }
      else
      {
        $lastmodifiedtimestamp = filemtime( $name );
        $dirlist[] = array( "name" => $name, "lastmodifiedtimestamp" => $lastmodifiedtimestamp );
      }

      $name = readdir( $currentdirectoryhandle );
    }

    closedir( $currentdirectoryhandle );
    } //end if


    foreach ( $dirlist as $key => $row )
    {
    $dirnamecolumn[ $key ] = $row[ "name" ];
    $dirlastmodifiedtimestampcolumn[ $key ] = $row[ "lastmodifiedtimestamp" ];
    }

    foreach ( $filelist as $key => $row )
    {
    $filenamecolumn[ $key ] = $row[ "name" ];
    $filelastmodifiedtimestampcolumn[ $key ] = $row[ "lastmodifiedtimestamp" ];
    }



    print "<html><body> \n";
    print "<form action=\"" . $_SERVER['PHP_SELF'] . "\"" . " method=\"post\"> \n" ;
    print '<input type="submit" name="sortbyname" value="sortbyname">' . "\n" ;
    print '<input type="submit" name="sortbytimestamp" value="sortbytimestamp">' . "\n" ;
    print "\n</form> \n" ;


    if( isset( $_REQUEST['sortbytimestamp'] ) && !empty( $_REQUEST['sortbytimestamp']) )
    {
        array_multisort( $dirlastmodifiedtimestampcolumn, SORT_DESC, $dirnamecolumn, SORT_ASC, $dirlist);
        displayListing( $dirlist );
        print "<br\>&nbsp; <br\>&nbsp; <br\>&nbsp;\n";
        array_multisort( $filelastmodifiedtimestampcolumn, SORT_DESC, $filenamecolumn, SORT_ASC, $filelist);
        displayListing( $filelist );
    }
    else
    {

        array_multisort( $dirnamecolumn, SORT_ASC, $dirlist);
        displayListing( $dirlist );
        print "<br\>&nbsp; <br\>&nbsp; <br\>&nbsp;\n";
        array_multisort( $filenamecolumn, SORT_ASC, $filelist);
        displayListing( $filelist );
    }

    print "<br />\n</html></body>\n";
    include( $_SERVER["DOCUMENT_ROOT"] . "/includes/foot.txt");
?>

  • « arrays multi dimension array sort
  • Amazon dns route53 dns continued mx records »

Published

Jun 13, 2011

Category

php

~219 words

Tags

  • column 4
  • filetime 4
  • multi 8
  • php 82
  • self 5
  • sort 11
  • submit 4