Link to home
Start Free TrialLog in
Avatar of dragboatrandy
dragboatrandy

asked on

PHP File Exists with a wildcard

I have a page that requires a loop to loop through pictures of a specific listing.  The previously uploaded and are prefixed with that ID of a particular listing.  IE. ListingID = 10284, so the image files are something like this 10284_1.jpg, 10284_2.jpg, etc.  What I need to do is use the file exists function in php to find out how many files are in given folder with that prefix (in this case 10284).  I have searched but can't find a solution for this particular problem.
ASKER CERTIFIED SOLUTION
Avatar of glcummins
glcummins
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of dragboatrandy
dragboatrandy

ASKER

Can I use a value from a database like this?

<?php $arrImages = glob('../'.$row_rsListings['ListingID'].'_*.jpg');

echo count($arrImages);
?>
Certainly, as long as $row_rsListings['ListingID'] evaluates to a valid string.
So a bit of confusion for me...

If I have code that in html looks like:
<div class="zip">
<a href="code123.zip"><img src="/images/zip.gif" alt="Download Zip"></a>
</div>

and instead of hard-coding the code123 name I want just grab the zip in the current directory then it would be:

<div class="zip">
<?php
$zip = glob('./*.zip');
echo "<a href=\"$zip\"><img src=\"/images/zip.gi\f" alt=\"Download Zip\"></a>
?>
</div>
<div class="zip">
<?php
$zip = glob('./*.zip');
echo "<a href=\"$zip\"><img src=\"/images/zip.gi\f" alt=\"Download Zip\"></a>
?>
</div>

Open in new window

After a few typos and such this is the horrid code that I hacked.

I just want to ensure that there is just one zip file if there is allow the download else error
<?php
$zipcnt = glob("*.zip");
 
//echo "Zip = $zip";
echo "Count = ".count($zipcnt)."\n";
 
if (count($zipcnt) == 1) {
   foreach (glob("*.zip") as $zip);
   echo "<div class=\"zip\">";
   echo "<a href=\"".$zip."\"><img src=\"/images/zip.gif\" alt=\"Download Zip\"></a>";
   echo "</div>";
} else {
   echo "zip count is wrong ".count($zipcnt);
}
?>

Open in new window