tommybfisher Posted July 30, 2011 Share Posted July 30, 2011 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); Quote Link to comment https://forums.phpfreaks.com/topic/243267-how-to-select-the-last-file-in-a-directory-but-ignore-it-if-it-is-a-directory/ Share on other sites More sharing options...
gizmola Posted July 30, 2011 Share Posted July 30, 2011 That would not work if there are multiple directories. This would be a better approach: $lastfile = end($files); while (is_dir($files)) { $lastfile = prev($files); } Quote Link to comment https://forums.phpfreaks.com/topic/243267-how-to-select-the-last-file-in-a-directory-but-ignore-it-if-it-is-a-directory/#findComment-1249372 Share on other sites More sharing options...
tommybfisher Posted July 30, 2011 Author Share Posted July 30, 2011 yes that makes sense. However, it is still not working. When I echo out $lastfile, I am getting the name of the directory 'thumbs' I can't see anything else in the code causing this. Quote Link to comment https://forums.phpfreaks.com/topic/243267-how-to-select-the-last-file-in-a-directory-but-ignore-it-if-it-is-a-directory/#findComment-1249373 Share on other sites More sharing options...
gizmola Posted July 30, 2011 Share Posted July 30, 2011 I had a bug in my code snippet at very least. It should have been while is_dir($lastfile). Quote Link to comment https://forums.phpfreaks.com/topic/243267-how-to-select-the-last-file-in-a-directory-but-ignore-it-if-it-is-a-directory/#findComment-1249375 Share on other sites More sharing options...
tommybfisher Posted July 30, 2011 Author Share Posted July 30, 2011 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); } Quote Link to comment https://forums.phpfreaks.com/topic/243267-how-to-select-the-last-file-in-a-directory-but-ignore-it-if-it-is-a-directory/#findComment-1249376 Share on other sites More sharing options...
gizmola Posted July 30, 2011 Share Posted July 30, 2011 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; Quote Link to comment https://forums.phpfreaks.com/topic/243267-how-to-select-the-last-file-in-a-directory-but-ignore-it-if-it-is-a-directory/#findComment-1249585 Share on other sites More sharing options...
tommybfisher Posted July 31, 2011 Author Share Posted July 31, 2011 I have it working now, thanks. However, I don't fully understand why. If we use chdir to change the directory to what it was anyway, how does this work? Thanks for all the advice. Quote Link to comment https://forums.phpfreaks.com/topic/243267-how-to-select-the-last-file-in-a-directory-but-ignore-it-if-it-is-a-directory/#findComment-1249749 Share on other sites More sharing options...
gizmola Posted July 31, 2011 Share Posted July 31, 2011 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. Quote Link to comment https://forums.phpfreaks.com/topic/243267-how-to-select-the-last-file-in-a-directory-but-ignore-it-if-it-is-a-directory/#findComment-1249923 Share on other sites More sharing options...
tommybfisher Posted July 31, 2011 Author Share Posted July 31, 2011 Got it! Thanks for the thorough explanation! Quote Link to comment https://forums.phpfreaks.com/topic/243267-how-to-select-the-last-file-in-a-directory-but-ignore-it-if-it-is-a-directory/#findComment-1249924 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.