Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 2869
  • Last Modified:

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.
0
dragboatrandy
Asked:
dragboatrandy
  • 2
  • 2
1 Solution
 
glcumminsCommented:
<?php

$arrImages = glob('10284_*.jpg');

echo count($arrImages);

?>
0
 
dragboatrandyAuthor Commented:
Can I use a value from a database like this?

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

echo count($arrImages);
?>
0
 
glcumminsCommented:
Certainly, as long as $row_rsListings['ListingID'] evaluates to a valid string.
0
 
bs98909Commented:
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

0
 
bs98909Commented:
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

0

Featured Post

VIDEO: THE CONCERTO CLOUD FOR HEALTHCARE

Modern healthcare requires a modern cloud. View this brief video to understand how the Concerto Cloud for Healthcare can help your organization.

  • 2
  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now