Lodius2000 Posted July 3, 2008 Share Posted July 3, 2008 Is there a function that returns the filenames in a folder as an array?, what is it? Link to comment https://forums.phpfreaks.com/topic/113015-solved-quick-and-easy/ Share on other sites More sharing options...
webent Posted July 3, 2008 Share Posted July 3, 2008 Will this work for ya, I use it all the time... $pattern="(\.csv$)|(\.CSV$)"; $files = array(); if ($handle = opendir($dirname)) { while(false !== ($file = readdir($handle))) { if(eregi($pattern, $file)) // Here is your array --> $file closedir($handle); } } Link to comment https://forums.phpfreaks.com/topic/113015-solved-quick-and-easy/#findComment-580520 Share on other sites More sharing options...
Lodius2000 Posted July 3, 2008 Author Share Posted July 3, 2008 i follow except for the first line: $pattern="(\.csv$)|(\.CSV$)"; what does it do, pit a | inbetween file names? Link to comment https://forums.phpfreaks.com/topic/113015-solved-quick-and-easy/#findComment-580526 Share on other sites More sharing options...
ofs Posted July 3, 2008 Share Posted July 3, 2008 no, the | character means "OR", so it's checking for both lowercase and uppercase file extensions. Link to comment https://forums.phpfreaks.com/topic/113015-solved-quick-and-easy/#findComment-580531 Share on other sites More sharing options...
Lodius2000 Posted July 3, 2008 Author Share Posted July 3, 2008 im an idiot, its a regular expression in that case can you tell me what it does //no good with reg expressions Link to comment https://forums.phpfreaks.com/topic/113015-solved-quick-and-easy/#findComment-580533 Share on other sites More sharing options...
webent Posted July 3, 2008 Share Posted July 3, 2008 You can define the types of files that you want to get... .csv, .jpg, etc. Link to comment https://forums.phpfreaks.com/topic/113015-solved-quick-and-easy/#findComment-580536 Share on other sites More sharing options...
Lodius2000 Posted July 3, 2008 Author Share Posted July 3, 2008 so i could just switch it to jpg, if i am searching for photos?... it cant be that easy heh Link to comment https://forums.phpfreaks.com/topic/113015-solved-quick-and-easy/#findComment-580540 Share on other sites More sharing options...
ofs Posted July 3, 2008 Share Posted July 3, 2008 $pattern="(\.csv$)|(\.CSV$)"; 1. The slash is escaping the dot that follows it, because we want it to actually look for the dot (a dot in regex translates basically to "anything". 2. The "csv" is just the file-type (can also be .php, .txt, etc.) 3. the $ symbol tells the regex function to search at the end of the string (the string being, in this case, the full file name). 4. the | symbol says, "OR" 5. The check is done again for an uppercase file extension... later in the code, webent used the "eregi" function, which performs a case-insensitive regex search, but I guess he was just being extra-thorough. 6. The parentheses are just to group each section of the expression. webent, correct me if something I said is wrong. edit: and, yes, it is that easy. Link to comment https://forums.phpfreaks.com/topic/113015-solved-quick-and-easy/#findComment-580541 Share on other sites More sharing options...
webent Posted July 3, 2008 Share Posted July 3, 2008 ofs, couldn't of said it better myself... Link to comment https://forums.phpfreaks.com/topic/113015-solved-quick-and-easy/#findComment-580542 Share on other sites More sharing options...
webent Posted July 3, 2008 Share Posted July 3, 2008 That's correct. so i could just switch it to jpg, if i am searching for photos?... it cant be that easy heh Link to comment https://forums.phpfreaks.com/topic/113015-solved-quick-and-easy/#findComment-580546 Share on other sites More sharing options...
Lodius2000 Posted July 3, 2008 Author Share Posted July 3, 2008 suite, thanks Link to comment https://forums.phpfreaks.com/topic/113015-solved-quick-and-easy/#findComment-580551 Share on other sites More sharing options...
Lodius2000 Posted July 3, 2008 Author Share Posted July 3, 2008 so here is my attempt, I am trying to count the number of instances of date('mjy') that appear in a folder so that I can add 1 to it and make a new file that is numbered sequentially, but use the day to separate the files for ease of archiving i have it pointing to a folder that has 10 files with the digits 070208 in it and this script returns 1 <?php $dirname = "blah2/"; $pattern="(\.csv$)|(\.CSV$)"; $files = array(); if ($handle = opendir($dirname)) { while(false !== ($file = readdir($handle))) { if(eregi($pattern, $file)) // Here is your array --> $file closedir($handle); } } $date = date('mjy'); $datepattern = "(" . $date . ")"; print $count; //for debugging $count = count(eregi($datepattern, $file)); if ($count >= 0) { $i = ($count+1); } //$i goes on to make the next file in my upload script ?> Link to comment https://forums.phpfreaks.com/topic/113015-solved-quick-and-easy/#findComment-580598 Share on other sites More sharing options...
kenrbnsn Posted July 3, 2008 Share Posted July 3, 2008 Take a look at the glob() function. It does what you want with very little code. Here's an example taken from the above page: <?php foreach (glob("*.txt") as $filename) { echo "$filename size " . filesize($filename) . "\n"; } ?> Here's some code for doing what you want: <?php $count = count(glob('*' . date('mjy') . '*.*')); echo $count .'<br>'; $count++; ?> Ken Link to comment https://forums.phpfreaks.com/topic/113015-solved-quick-and-easy/#findComment-580603 Share on other sites More sharing options...
Lodius2000 Posted July 3, 2008 Author Share Posted July 3, 2008 Ken I considered glob but couldnt figure out a way to make it count, the integer that matches the number of times the string appears in the array made from the file contents of the folder is what I want basically if there are 10 files in a folder called blah2 that contain the string 070208 as part of their filename i want $count to return 10 // i noticed that date('mjy') will return 07208, i have since changed it to 'mdy' Link to comment https://forums.phpfreaks.com/topic/113015-solved-quick-and-easy/#findComment-580607 Share on other sites More sharing options...
kenrbnsn Posted July 3, 2008 Share Posted July 3, 2008 See my second example that I put in my response. Ken Link to comment https://forums.phpfreaks.com/topic/113015-solved-quick-and-easy/#findComment-580614 Share on other sites More sharing options...
Lodius2000 Posted July 3, 2008 Author Share Posted July 3, 2008 ken so the * is a wildcard, telling glob that anything (or nothing) can exist so your line '*' . date('mjy') . '*.*' means 'anything (or nothing)' the date 'anything (or nothing)'.'any-file-extention', or is the first * where the path goes? Link to comment https://forums.phpfreaks.com/topic/113015-solved-quick-and-easy/#findComment-580619 Share on other sites More sharing options...
kenrbnsn Posted July 3, 2008 Share Posted July 3, 2008 Yes, the "*" is a wild character which will match zero or more characters, so the pattern will match 070208.txt something070208.somethingelse xyz070208abc.12345 If you want to look in another directory, just put that directory and a "/" in front of the string: <?php $count = count(glob('testarea/*' . date('mdy') . '*.*')); echo $count .'<br>'; $count++; echo $count; ?> Ken Link to comment https://forums.phpfreaks.com/topic/113015-solved-quick-and-easy/#findComment-580621 Share on other sites More sharing options...
Lodius2000 Posted July 3, 2008 Author Share Posted July 3, 2008 the simplicity of this language continues to amaze me thanks for all of your help Link to comment https://forums.phpfreaks.com/topic/113015-solved-quick-and-easy/#findComment-580625 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.