It is necessary to display pictures from folders in a loop, until it goes through everything. I tried to use the file_exists function , which checks the existence of the specified file or directory and thus, will perform a loop until the files in the specified directory end, but for some reason the construction does not work . The code 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 = $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:

 <?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>"; ?> 

Answer the author of the question:
It was necessary to realize the output of pictures from a specific folder, but before that determine how many of them are there. The output was found, maybe a little curve, but it works fine! <? 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 "

";

?>

  • Dear friend, it may not work if, for example, you have a file named 1.jpg . Well, or 2.jpg (then it will work once). But that, in your understanding, "does not work," science is unknown. - Ali
  • And that also means that displays nothing though such files are. Although if you think it does not give errors, then it works, then yes! - cheh1
  • Write the result of this: $ img = 1; $ imgf = "/ files / id $ id /". $ img. ".jpg"; print_r (scandir ($ imgf)); print_r (scandir ($ _ SERVER ['DOCUMENT_ROOT']. $ imgf)); print_r (realpath ($ imgf)); - Sh4dow

3 answers 3

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

We look, we stick in, we decide what to do with the result. ;)

  • =. = About you I have already written in my profile. - knes
  • Well, no one has offered this feature. And this is another + option. =) - ling
  • Here are the plus for the option. - knes
  • Thank). So this is how, it turns out, the plus-dragging effect works ... - ling
  • It works this way: Ling comes, draws an abstruse, short answer that works ... and pulls the plus signs. =. = - knes
 <?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; } ?> 

Do ... And the file_exists function really checks for the existence of a file. Only you, it checks the existence of only one file.

  • I now fixed the node, look! It seems everything is correct, first for $ img I assign 1, the information is displayed, then increment by 1 checks the existence of the file displays, etc. or not? - cheh1
  • img something you change. And imgf is not. It is necessary to change imgf at each iteration of the loop, since this variable is checked while (file_exists ($ imgf)) {// Your code $ img = $ img +1; $ imgf = "/ files / id $ id /". $ img. ".jpg"; } - knes
  • In my opinion, the author of the question is non-recoverable. Or a troll)) - Ali
  • Remove $ img = 1 at the beginning of the loop. Otherwise, the cycle will end only after 30 seconds. - knes
  • What for inside the loop $img = 1; stuck? Again, you expose the same value. - Ali

Inside the $imgf update. And then you have a constant condition, all the time the same file checks.

It is better to use readdir / scandir . After all, in the general case - to which number will you go through the files?

  • I apologize for the code was not the one registered. Corrected! - cheh1
  • The problem is the same. - Ali