Jump to content

[SOLVED] Order array of files made with glob()


ashton321

Recommended Posts

Hi,

 

I have an array of file names to be used in another script created using glob() is there anyway to order the list by the date modified on the server?

 

<?php $query = mysql_query("SELECT * FROM galleries");
                        $result = mysql_fetch_array($query);
                        $galname = $result['galname'];
                        $files = array();
                                foreach(glob("../webdev/images/galleries/$galname/*") as $file)
                                {
                                        if(!is_dir($file))
                                        {
                                                $files[] = array("path" => $file);
                                        }
                                }
                        
                        // echo $files[0]["name"];
                        $firstfile = @ltrim($files[0]["path"], "../webdev/");
                        $firstfile = '../'.$firstfile;
                        echo '<a href="../scripts/phpThumb.php?src='.$firstfile.'&h=400&w=400" rel="lightbox-'.$galname.'" ><img src="../scripts/phpThumb.php?src='.$firstfile.'&h=200&w=200"</a>';
                        foreach ($files as $pic)
                        {
                                foreach ($pic as $path)
                                {
                                        if ($path != $files[0]["path"])
                                        {                                        
                                        $path =  @ltrim($path, "../webdev/");
                                        $path = '../'.$path;
                                        echo '<a href="../scripts/phpThumb.php?src='.$path.'&h=400&w=400" rel="lightbox-'.$galname.'" ></a>';
                                        }
                                        else {}
                                }        
                        }  ?>

Link to comment
Share on other sites

Not tested:

<?php

$query = mysql_query("SELECT * FROM galleries");

$result = mysql_fetch_array($query);
$galname = $result['galname'];

$files = array();
foreach(glob('../webdev/images/galleries/$galname/*') as $file)
{
    if(is_file($file))
    {
        $files[] = array(
            'path'          => '../' . ltrim($file, '../webdev/'),
            'date_modified' => filemtime($file);
        );
    }
}
                    
//Order the array by date modified
function sortByModified()
{
    if ($a['date_modified'] == $b['date_modified']) {
        return 0;
    }
    return ($a['date_modified'] < $b['date_modified']) ? -1 : 1;
}
usort($files, 'sortByModified');

//print the results
echo "<a href=\"../scripts/phpThumb.php?src={$files[0]['path']}&h=400&w=400\" rel=\"lightbox-{$galname}\" >
echo "<img src=\"../scripts/phpThumb.php?src={$files[0]['path']}&h=200&w=200\"</a>";

foreach ($files as $file)
{
    echo "<a href=\"../scripts/phpThumb.php?src={$file['path']}&h=400&w=400\" rel=\"lightbox-{$galname}\" ></a>";
}

?>

Link to comment
Share on other sites

Ok, I understand that(should have looked for the filetime function), now how would i apply that to my foreach loop that displays the result?  I know if it were SQL you would date ORDER by date_modified DESC but i am assuming its not that simple for a foreach loop.  I am just beginning to use foreach loops in my coding so im not exactly sure what to do.

 

here is how i display the result currently

<?php $firstfile = @ltrim($files[0]["path"], "../webdev/");
                        $firstfile = '../'.$firstfile;
                        echo '<a href="../scripts/phpThumb.php?src='.$firstfile.'&h=400&w=400" rel="lightbox-'.$galname.'" ><img src="../scripts/phpThumb.php?src='.$firstfile.'&h=200&w=200"</a>';
                        foreach ($files as $pic)
                        {
                                foreach ($pic as $path)
                                {
                                        if ($path != $files[0]["path"])
                                        {
                                        $path =  @ltrim($path, "../webdev/");
                                        $path = '../'.$path;
                                        echo '<a href="../scripts/phpThumb.php?src='.$path.'&h=400&w=400" rel="lightbox-'.$galname.'" ></a>';
                                        }
                                        else {}
                                }
                        } ?>

Link to comment
Share on other sites

In the code I provided, it would create a multi-dimensional array for each file which would include the path and the file modified date. Then there is a small block of code that would order the array. Then you just do a foreach loop to echo the results.

 

As I stated that code was not tested - but the logic was sound. I fixed a few minor errors and it works fine now. Give this a try:

 

<?php

$query = mysql_query("SELECT * FROM galleries");

$result = mysql_fetch_array($query);
$result['galname'];

$files = array();
foreach(glob("../webdev/images/galleries/$galname/*") as $file)
{
    if(is_file($file))
    {
        $files[] = array(
            'path'          => $file,
            'name'          => basename($file),
            'date_modified' => filemtime($file)
        );
    }
}

//Order the array by date modified
function sortByModified()
{
    if ($a['date_modified'] == $b['date_modified']) {
        return 0;
    }
    return ($a['date_modified'] < $b['date_modified']) ? -1 : 1;
}
usort($files, 'sortByModified');

//print the results
echo "<a href=\"../scripts/phpThumb.php?src={$files[0]['path']}&h=400&w=400\" rel=\"lightbox-{$galname}\" >";
echo "<img src=\"{$files[0]['path']}\"</a><br /><br />\n";

foreach ($files as $file)
{
    echo "<a href=\"../scripts/phpThumb.php?src={$file['path']}&h=400&w=400\" rel=\"lightbox-{$galname}\" >{$file['name']}</a><br />\n";
}

?>

Link to comment
Share on other sites

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.