monkeytooth Posted January 8, 2010 Share Posted January 8, 2010 file_exists with wildcard for extension. Is it possible to do a file_exists without an extension? I ask because I have about 750,000 if not more images, all of which follow a specific file_name format, that matches a key in a database I am working with. however, not all the formats are the same, I have gif, jpg, png, and I think maybe even a couple random .bmp. So I am trying to figure out if its possible to do anything like file_exists but without the extension, so that if it finds the file without the extension it will append the file type to the end of it so I can then call it out in html with img src... Anyone know if its possible? Can I use file_exists for something like this or am I thinking to much in the box with this, and need to broden my perspective of how I should build a function like this. I'm open to any ideas, suggestions, appreciate the help. Quote Link to comment https://forums.phpfreaks.com/topic/187679-file_exists-with-wildcard-for-extension/ Share on other sites More sharing options...
cags Posted January 8, 2010 Share Posted January 8, 2010 I'm not certain as I've never done it but this should be achievable with glob. Generally speaking examples will be in the format '*.txt', but I see no reason you can't do the opposite, 'something.*'. Quote Link to comment https://forums.phpfreaks.com/topic/187679-file_exists-with-wildcard-for-extension/#findComment-990810 Share on other sites More sharing options...
Zane Posted January 8, 2010 Share Posted January 8, 2010 My first instinct would be to create an array of all the possible extensions you want to narrow down and create the file name (to be placed as the file_exists parameter) dynamically.....with a loop. So you'd loop through all 750k+ filenames and within that loop.. check for each extension using the extension array. psuedo-code $extensions[] = "bmp"; $extensions[] = "png"; $extensions[] = "jpg"; $extensions[] = "jpeg"; $extensions[] = "gif"; while($row = mysql_fetch_array($imageQuery)) { foreach($extensions as $ext) { $file2check = $row['filename'] . "." . $ext; if(file_exists($file2check)) echo $file2check . " exists! "; } } Though that method may be slow, it's the only thing that comes to mind at this point. There may be a faster way...using glob or some kind of regex matching, but it's up to you which method you want. Quote Link to comment https://forums.phpfreaks.com/topic/187679-file_exists-with-wildcard-for-extension/#findComment-990813 Share on other sites More sharing options...
salathe Posted January 8, 2010 Share Posted January 8, 2010 The solution will vary depending on precisely what you're wanting to do. Do you want to get one key from the database and see if there is a (jpg, gif, whatver) file matching that key, or do you want to do similar with many (all?) keys, or..? Quote Link to comment https://forums.phpfreaks.com/topic/187679-file_exists-with-wildcard-for-extension/#findComment-990856 Share on other sites More sharing options...
monkeytooth Posted January 8, 2010 Author Share Posted January 8, 2010 Well I am building up a search function, and pulling the results from the DB and listing them accordingly. One certain key per row is equivalent to a particular image in a folder else where. However when who ever put this DB together put it together there wasn't apparently images for it, it was just the info, as is. Later someone else came along and started naming images to match that particular key, and from there the standard was set. Now However long since that standard was set for key and image. There is 750k+ of images that match that key, now normally that even wouldnt be a problem. However Over time people have used diffrent image formats gif, jpg, png, bmp, in both upper and lower case. So the issue at hand is the key fits but don't know what to append to it for the img src tag. So I have to A find the extension to verify the file exists, and if so put it in the img src. If it doesn't then use a default image "No Image Available" type of image. What I have thus far.. is while($results = mysql_fetch_array($display_result)) { $booktitle = $results['Title']; $Product10 = $results['Product10']; $Product13 = $results['Product13']; $author = $results['Author']; $book_img = $results['Product13']; $extensions[] = "JPG"; $extensions[] = "GIF"; foreach($extensions as $ext) { $file2check = $Product13 . "." . $ext; if(file_exists($_SERVER['DOCUMENT_ROOT'].'/V20/book_img/'.$file2check)){/*$imgOut = $file2check;*/$imgOut = "[img found]";}else{/*$imgOut = "no_image.jpg";*/ $imgOut = "[no image]";} echo $_SERVER['DOCUMENT_ROOT']; echo "<img src=\"book_img/". $file2check ."\" /><br />"; } echo $imgOut . ' <strong>Product10: </strong><a href="/book/Product10/' . $Product10 . '" title="Product10: ' . $Product10 . '">' . $Product10 . '</a> -- <strong>Product13: </strong><a href="/book/Product13/' . $Product13 . '" title="Product13: ' . $Product13 . '">' . $Product13 . '</a> -- <strong>Author: </strong><a href="/book/author/' . $author . '" title="Author: ' . $author . '">' . $author .'</a> -- <strong>Title: </strong><a href="/book/title/' . $booktitle . '" title="Book Title: ' . $booktitle . '">' . $booktitle .'</a><br />'; }//end loop I am running into issues, with it. I don't know what specificly. It finds the extnsions now, but doesnt return a result, in this cae i keep getting the false "[no image]" Quote Link to comment https://forums.phpfreaks.com/topic/187679-file_exists-with-wildcard-for-extension/#findComment-991181 Share on other sites More sharing options...
salathe Posted January 8, 2010 Share Posted January 8, 2010 You'll probably want to break out of the foreach loop upon finding a successful match by using the keyword continue. Quote Link to comment https://forums.phpfreaks.com/topic/187679-file_exists-with-wildcard-for-extension/#findComment-991232 Share on other sites More sharing options...
monkeytooth Posted January 9, 2010 Author Share Posted January 9, 2010 You know, I came to the conclusion it was that foreach loop, but couldnt figure out how to break it. That continue helps, thank you. Quote Link to comment https://forums.phpfreaks.com/topic/187679-file_exists-with-wildcard-for-extension/#findComment-991490 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.