PHP includes image file with file name change

I am completely new to PHP, so forgive me if this question seems very rudimentary. And thank you in advance.

I need to enable jpg, which is created from a webcam on another page. However, I need to include only the latest jpg file. Unfortunately, the webcam creates a unique file name for each jpg. How can I use include or another function to include only the last image file? (Usually the file name looks like this 2011011011231101.jpg, where the value is year_month_date_timestamp).

+3
source share
5 answers

A simple way is to get the latest image using the code below

$path = "/path/to/my/dir"; 

$latest_ctime = 0;
$latest_filename = '';    

$d = dir($path);
while (false !== ($entry = $d->read())) {
  $filepath = "{$path}/{$entry}";
  // could do also other checks than just checking whether the entry is a file
  if (is_file($filepath) && filectime($filepath) > $latest_ctime) {
      $latest_ctime = filectime($filepath);
      $latest_filename = $entry;
    }
  }
}

// now $latest_filename contains the filename of the newest file

<img>

+2

, , :

$imgs = glob('C:\images\*.jpg');
rsort($imgs);
$newestImage = $imgs[0];
+2

, , . , - , , , - ( - , ), , script, php.

+1

, .

, , . readdir (doc) - , . script, : http://www.liamdelahunty.com/tips/php_list_a_directory.php

substr() (doc), .

, . sort (doc) SORT_NUMERIC. , .jpg, .

: , . , , , , , , .

+1

@ken, , . , :

$imgs = glob('C:\images\*.jpg');
rsort($imgs, SORT_NUMERIC);
$newestImage = $imgs[0];
+1

Source: https://habr.com/ru/post/1784796/


All Articles