Jump to content

Recommended Posts

Hello everyone,
Could someone take a quick look at my code for me? I'm trying to scan a folder and list the highest file (by filename) as a link to that file. I've managed to list them in descending order but when I add the array_shift() function it removes the top value instead of all the others.

eg. I can list files called "f4.pdf, b5.pdf, a3.pdf, c1.pdf" in the order "f4.pdf, c1.pdf, b5.pdf, a3.pdf" and make them links to the relevant files, but I need it limit the results to the top value "f4.pdf". With the code below I get "c1.pdf, b5.pdf, a3.pdf".

Hope thats not too confusing!


[code]
$thefiles = array(); //initialize
if ($handle = opendir('.')) {
  while (false !== ($file = readdir($handle))) {
      if ($file != "." && $file != ".." && $file != "index.php" && $file != "test.php" && !is_dir($file)) {
            //use filesystem functions to get the filesize and other attributes RTFM
            $thefiles[] = array('filename' => $file);
            //used this way, it just adds the new item to the end of the array
            // it is creating an array of arrays
      }
  }
  closedir($handle);
}
//at this point the $thefiles array contains all the file info
//access it using 2 indexes like this  echo $thefiles[0]['filetype'];
//use array functions to sort $thefiles array to your pleasure RTFM
//use the foreach() to cycle through the $thefiles array and echo the files and other attributes out

krsort($thefiles);
//arrange descending by filename

array_shift($thefiles);

foreach ($thefiles as $singlefile) {
$Doc = $singlefile['filename'];
    echo "<a class='Body2' href='$Doc'>$Doc</a><br>";
}
[/code]
Link to comment
https://forums.phpfreaks.com/topic/20619-find-top-value-in-an-array/
Share on other sites

OK that's really confused me!! I've added the code you mentioned but now when I refresh the page it just says "Array" (which is NOT one of the file names). Could one of you take a look and see if I've implemented the code incorrectly?



[code]
$thefiles = array(); //initialize
if ($handle = opendir('.')) {
  while (false !== ($file = readdir($handle))) {
      if ($file != "." && $file != ".." && $file != "index.php" && $file != "test.php" && !is_dir($file)) {
            //use filesystem functions to get the filesize and other attributes RTFM
            $thefiles[] = array('filename' => $file);
            //used this way, it just adds the new item to the end of the array
            // it is creating an array of arrays
      }
  }
  closedir($handle);
}
//at this point the $thefiles array contains all the file info
//access it using 2 indexes like this  echo $thefiles[0]['filetype'];
//use array functions to sort $thefiles array to your pleasure RTFM
//use the foreach() to cycle through the $thefiles array and echo the files and other attributes out

krsort($thefiles);
//arrange descending by filename

$number_holder = count($thefiles) - 1; 

echo $thefiles[$number_holder];
[/code]
[code]<?php
$path = realpath('/path/to/directory');
$ds = DIRECTORY_SEPARATOR;
$handle = opendir($path);
$file = array();

while (($file = readdir($handle)) !== false)
{
    if (!in_array($file, array('.', '..')) && is_file($path . $ds . $file))
    {
        $files[] = array('filename' => $file);
    }
}

// get last array element.
$lastFile = array_pop($files);

echo htmlentities($lastFile['filename']);

?>[/code]
Jenk's code will also work, but does something else behind the scenes that you aren't aware of.

array_pop() actually removes the last item from the array.  So although you get the value that you're after returned.  Your $thefiles array now only contains 3 items instead of 4.... I think  :-\

Regards
Huggie
You are correct, it does.

There is also another variant to this solution..

[code]<?php

while (($file = readdir($handle)) !== false)
{
    if (!in_array($file, array('.', '..')) && is_file($path . $ds . $file))
    {
        $final_file = array('filename' => $file);
    }
}

echo htmlentities($final_file['filename']);

?>[/code]
This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.