mhoctober Posted May 2, 2006 Share Posted May 2, 2006 All,my users load images, which I store and partly rename - I add a number to the front of the image name eg. myboata.jpg and myboatb.jpg become for instance 12334(1)myboata.jpg, 12334(2)myboatb.jpg etc... I then want to search for all instances of files that commence with 12334 and am trying the following...$pathAndFile = "myfiles/12334*"if (file_exists($pathAndFile)) { echo "The file exists";} else { echo "The file does not exist";}in the folder myfiles there are files that begin with 12334, but the message I get back from the If statement is telling me that "The File Does Not Exist"I have also tried $pathAndFile = "myfiles/12334*.*"and get the same result.Is it legal to use wildcards in this way with the file_exists function?If not - does anyone have any idea how I can serach for files where only the first 5 characters of the filename will ever be known?Many thanks.. Link to comment https://forums.phpfreaks.com/topic/8917-file_exists-and-wildcards/ Share on other sites More sharing options...
litebearer Posted May 2, 2006 Share Posted May 2, 2006 I didn't have time to test it, but this should find files whose names contain your search criteria.[code]<?php$needle = "12345"; // this is the stuff (string/name/etc) we are looking forif ($handle = opendir('/path/to/files')) { while (false !== ($file = readdir($handle))) { if ($file != "." && $file != "..") { $pos = strpos($file, $needle); if($pos === false) { // not found }else{ // you found a file now do what you want ) } } closedir($handle);}?> [/code]Also you could modify the code to check if the first 5 character match the needle, by using the substr() functionLite... Link to comment https://forums.phpfreaks.com/topic/8917-file_exists-and-wildcards/#findComment-32754 Share on other sites More sharing options...
kenrbnsn Posted May 3, 2006 Share Posted May 3, 2006 A much easier way of doing this would be to use the [a href=\"http://www.php.net/glob\" target=\"_blank\"]glob()[/a] function:[code]<?php$chk_files = glob('myfiles/12334*.jpg');if (emtpy($chk_files)) echo 'No files found';?>[/code]Ken Link to comment https://forums.phpfreaks.com/topic/8917-file_exists-and-wildcards/#findComment-33027 Share on other sites More sharing options...
mhoctober Posted May 3, 2006 Author Share Posted May 3, 2006 [!--quoteo(post=370975:date=May 3 2006, 01:01 PM:name=kenrbnsn)--][div class=\'quotetop\']QUOTE(kenrbnsn @ May 3 2006, 01:01 PM) [snapback]370975[/snapback][/div][div class=\'quotemain\'][!--quotec--]A much easier way of doing this would be to use the [a href=\"http://www.php.net/glob\" target=\"_blank\"]glob()[/a] function:[code]<?php$chk_files = glob('myfiles/12334*.jpg');if (emtpy($chk_files)) echo 'No files found';?>[/code]Ken[/quote]Fantastic ! Works a treat ! Thank God for this forum! Link to comment https://forums.phpfreaks.com/topic/8917-file_exists-and-wildcards/#findComment-33085 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.