Jump to content

How to select the last file in a directory, but ignore it if it is a directory


tommybfisher

Recommended Posts

I have a list of sequentially numbered photos in a folder ('uploads') There is a subdirectory within 'uploads' called 'thumbs'. I want to be able to select the last file in 'uploads' but currently 'thumbs' is being selected.

I'm relatively new to php, so I'm sure there's an easy way to ignore 'thumbs' but I'm not sure what it is. My attempt below was to select the last file, but if the last file was a directory then select the previous file. However this doesn't seem to work.

Could somebody please point me in the right direction?

Many thanks

 

/*get the current directory*/
$thisdir = getcwd();
/*access the uploads folder*/
$dir = $thisdir.'/uploads';
/*read the files in the uploads folder*/
$files = scandir($dir);
/*select the last file sequentially, but if dir, select previous*/
if(is_dir(end($files)))
$lastfile = prev($files);
else
$lastfile = end($files);

 

Link to comment
Share on other sites

I've updated the code, but still it is echoing out 'thumbs' as the last file.

Is there something else in the code that is causing this?

 

/*get the current directory*/
$thisdir = getcwd();
/*access the uploads folder*/
$dir = $thisdir.'/uploads';
/*read the files in the uploads folder*/
$files = scandir($dir);
/*select the last file sequentially*/
$lastfile = end($files);
while (is_dir($lastfile))
{
$lastfile = prev($files);
}

Link to comment
Share on other sites

Yeah the problem is with is_dir().  It operates relative to the cwd.  So it's actually failing and returning false because it can not find any of the files or directories.  Thus it never enters into the while(). You can append the path to the files, which depending on what you ultimately intend to do with them.  Just to get a quick fix add a chdir() call:

 

/*get the current directory*/
$thisdir = getcwd();
/*access the uploads folder*/
$dir = $thisdir.'/uploads';
/*read the files in the uploads folder*/
chdir($dir);
$files = scandir($dir);
/*select the last file sequentially*/
$lastfile = end($files);
echo "$lastfile\n";
while (is_dir($lastfile))
{
  $lastfile = prev($files);
  echo "$lastfile\n";
}
echo $lastfile;

 

 

Link to comment
Share on other sites

It actually isn't that directory though, the Current working directory is the one where the script is executing from.  You store that path in the $thisdir variable.  Then you append to that the /uploads and use that to scandir.  However your current working directory is still $thisdir.

 

So you can either change the CWD, or you can use the same method you used with scandir for is_dir and add the filename to the $thisdir."/uploads/" path when you pass the name to is_dir. 

 

 

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.