Jump to content

[SOLVED] geting last files from specific directory


prakash

Recommended Posts

Hi,

 

I have one directory which will contains images having numeric values (say 1.jpg, 2.jpg .......)

I wanted to get the filename of that image which is having highest numeric value within the dir.

If there is 1.jpg, 2.jpg, 3.jpg, 4.jpg the file having highest numeric value will be 4.jpg

 

is there any easy solution for this or how can I do it

 

I encounter problems with scandir($dir,1);

actually when I have files like 8.jpg, 9.jpg, 10.jpg, 11.jpg it actually display 9.jpg as a highest value or order.

so how to resolve this issue.

below is my code

 

<?php
$scanDir=scandir('images', 1);
$intFile=$scanDir[0];
print $intFile; // it prints 9.jpg where it should be 11.jpg
?>

<?php
  $a = '0004';
  $b = $a + 1; // $b is now 5
  print str_pad($b,4,STR_PAD_LEFT).".jpg";
?>

 

mmm just found the mistake from php.net site

str_pad($b, 4, STR_PAD_LEFT).".jpg" should be str_pad($b, 4, "0", STR_PAD_LEFT).".jpg";

 

correct me if I am wrong

and thanks for the help

Archived

This topic is now archived and is closed to further replies.

×
×
  • 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.