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

 

Link to comment
Share on other sites

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
?>

Link to comment
Share on other sites

You could name your files 0001.jpg, 0002.jpg, etc

 

that may be an another option.

but for using that I need the increment and decrement value for next and previous image.

so how could it be possible 0004+1=0005 or 0004-1=0003

Link to comment
Share on other sites

<?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

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.