Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Display an ordered in wordpress with EXEC-PHP

I'm working on a WordPress blog. The previous developers used "Exec-PHP" to execute a PHP script within some page.

The following one display on the http://url-of-the-page/ a list of files contained in the "/homez.406/xxx/www/wp-content/xxx/xxx/".

I would like to order by date the files but I don't know how to do it! Does someone already used this?

<!--?php showContent('/homez.406/xxx/www/wp-content/xxx/xxx/','http://url-of-the-page/',false,false); ?-->

This is what I found in the functions.php

function showContent($path,$webpath,$adminclear,$adminup){

if ($handle = opendir($path))
{
   if ($adminclear==true)

   {
    global $user_ID; if( $user_ID ) :
    if( current_user_can('level_10') ) :
    $auth=true;
    else : 
    $auth=false;
    endif; 
    endif; 
   }

   if ($adminup==true)

   {
    global $user_ID; if( $user_ID ) :
    if( current_user_can('level_10') ) :
    $authup=true;
    else : 
    $authup=false;
    endif; 
    endif; 
   }

   else{$auth=true;$authup=true;}



   if ((isset($_POST['dlfile']))&&($auth==true))
   {
   $removefile=$_POST['dlfile'];
    unlink ($removefile);

   }


   while (false !== ($file = readdir($handle)))
   {
       if ($file != "." && $file != "..")
       {
           $fName  = $file;
           $goodpath=$webpath.$fName;
           $file   = $path.$file;
           $abpath=$path.$fName;


           if(is_file($file)) {
               echo "<p><a href='http://www.otrmd.com/wp-content/themes/FactoryWP/dl.php?p=".$goodpath."&f=".$fName."'>".$fName."</a><br/> Uploaded on ".date ('d-m-Y H:i:s', filemtime($file))."<br/>Size: ".filesize($file)." bytes</p>";

               if($auth==true)
               {
               echo "<form method='post' action=".$_SERVER['REQUEST_URI'].">
               <input type='hidden' name='dlfile' value='".$abpath."'>
               <input type='submit' value='Clear File'>
               </form><br/>";
               }
           } elseif (is_dir($file)) {
               print "<p><a href='".$_SERVER['PHP_SELF']."?path=$file'>$fName</a></p><br/><br/>";
           }
       }
   }

   closedir($handle);
}    
if ($authup==true)
   {

   echo ("[uploadify folder='".$path."' multi=true]");

   }

}
like image 320
Fred Avatar asked Nov 12 '22 16:11

Fred


1 Answers

The problem here is that the function readdir is used, and the doc says :

The entries are returned in the order in which they are stored by the filesystem.

So I suggest to use scandir combined with uasort, to sort files by filemtime

Replace

while (false !== ($file = readdir($handle)))

By

$files = scandir($path);
uasort($files, 'sort_by_filemtime');
foreach ($files as $file) {
    ...

And declare the following callback function at the beginning of the script

function sort_by_filemtime($file1, $file2) {
    global $path;
    $file1mtime = filemtime($path.$file1);
    $file2mtime = filemtime($path.$file2);
    if ($file1mtime == $file2mtime) {
        return 0;
    }
    return $file1mtime > $file2mtime ? 1 : -1;
}
like image 153
mexique1 Avatar answered Nov 15 '22 06:11

mexique1