How does the file_exists function work?

It is necessary to display images from folders in a loop until it goes through everything. I tried to use the file_existsfunction, which checks the existence of the specified file or directoryand thus will loop until the files in the specified directory run out, but for some reason the construction does not work . The code is like this:

<?php echo "<div class='hidden-container'>";?>
<?php
$img = 1;
$imgf = "/files/id$id/" .$img. ".jpg";
while (file_exists($imgf))
{
echo "<a id='thumb1' class='highslide' href='/files/id$id/$img.jpg'
            onclick='return hs.expand(this, inPageOptions)'>
        <img src='../files/id$id/" .$img. "_thumb.jpg' alt='" . $myrow["title"] . "'/></a>"; 
$img = $img +1;
}
echo "</div>
<div id='gallery-area' style='width: 620px; height: 520px; margin: 0 auto; border: 1px solid silver'></div>";
?>

Attention, the correct answer is:

<?php echo "<div class='hidden-container'>";?>
<?php
$img = 1;
$imgf = "/files/id$id/" .$img. ".jpg";
while (file_exists($imgf)){
echo "<a id='thumb1' class='highslide' href='/files/id$id/$img.jpg'
            onclick='return hs.expand(this, inPageOptions)'>
        <img src='../files/id$id/" .$img. "_thumb.jpg' alt='" . $myrow["title"] . "'/></a>"; 
$img++;
$imgf = "/files/id$id/" .$img. ".jpg";
}
echo "</div>
<div id='gallery-area' style='width: 620px; height: 520px; margin: 0 auto; border: 1px solid silver'></div>";
?>

Questioner’s response:
It was necessary to implement the output of pictures from a certain folder, but before that, determine how many of them are there. There was a way out, maybe a little curve, but it works great!
<?php
$path = “files/id1/*_big.jpg”;
$m = glob($path);
$col = count($m);
$col = $col +1;
?>

“;?>
$img = 1;
$imgf = “/files/id$id/” .$img. “_big.jpg”;

while ($img < $col)
{

echo “
" . $myrow["title"] . "
“; $img = $img +1;

}
echo ”


Answer 1, authority 100%

$path = '/images/*.jpg';
$m = glob($path);
print_r($m);

We look, stick, decide what to do with the result. 😉


Answer 2, authority 100%

<?php
function getJpeg($path='/files/id$id/'){
$files = scandir($path);
$ret = array();
foreach($files as $file){
  if(preg_match('/\.jpg$/i',$file)){
    $ret[] = $path.$file;
  }
}
return $ret;
}
?>

Here… And the file_exists function actually checks for the existence of a file. Only for you, it checks for the existence of only one file.


Answer 3, authority 50%

Inside the $imgfloop, update. Otherwise, you get a constant condition, it checks the same file all the time.

Better to use readdir/scandir. After all, in the general case – up to what number will you sort through the files?