-2

Why am I getting an error stating that $image is not an array? And also I want to be able to sort the picture/files in the $image. I want to sort them by the time they were uploaded. First uploaded gets ontop of my page. These two issues have I been struggling with.

$files = glob("Pictures/*.*");
for ($i=0; $i<count($files); $i++)
{
    $image = $files[$i];
    sort($image);
    $supported_file = array(
        'gif',
        'jpg',
        'jpeg',
        'png'
        );

    $ext = strtolower(pathinfo($image, PATHINFO_EXTENSION));
    if (in_array($ext, $supported_file)) {
        echo basename($image)."<br />"; 
        echo '<img src="'.$image .'" alt="Random image" height="150" />';
        echo '<div class="content_wrapp">
Barmar
  • 741,623
  • 53
  • 500
  • 612
Rida
  • 1
  • And https://stackoverflow.com/questions/20930122/sort-glob-by-creation-date and https://stackoverflow.com/questions/124958/glob-sort-by-date and https://stackoverflow.com/questions/7948300/order-this-array-by-date-modified – mickmackusa Apr 28 '18 at 09:53

1 Answers1

0

You should be sorting $files, not $image. Use usort with a function that compares file times.

$files = glob("Pictures/*.*");
usort($files, function($a, $b) { return filemtime($b) - filemtime($a); });
Barmar
  • 741,623
  • 53
  • 500
  • 612