# Auto-Generating Filesystem-Based Gallery

**URL:** <https://www.phphelp.com/t/auto-generating-filesystem-based-gallery/12989>\
**Category:** Beginners - Learning PHP\
**Created:** [March 29, 2011, 10:02pm UTC](https://www.phphelp.com/t/auto-generating-filesystem-based-gallery/12989 "2011-03-29T22:02:07Z")\
**Posts on this page:** 2\
**Page:** 1

<div class="post-metadata">

**Author:** ![sternbz](https://www.phphelp.com/letter_avatar_proxy/v4/letter/s/8c91f0/32.png) [@sternbz](https://www.phphelp.com/u/sternbz)\
**Post date:** [March 29, 2011, 10:02pm UTC](https://www.phphelp.com/t/auto-generating-filesystem-based-gallery/12989/1 "2011-03-29T22:02:07Z")

</div>

So I am trying to create a gallery that calls up links based on folders, then in turn makes those links load that images in the script. My issue, is that since the page only ‘loads’ one time, the function generation works, but needs to be process one more time with the rest of the code in order to work (e.g. if i were to place the output directly into the page and make hard-links lower down in the script, the page would work… but the idea is not to have hard links). Help would be really appreciated as I have only worked with PHP in regards to editing WordPress themes…

Here’s the page:

> \<?php
> 
> $dir = “albums/”;
> 
> // Open a known directory, and proceed to read its contents  
> if (is\_dir($dir)) {  
> if ($dh = opendir($dir)) {  
> while (($album = readdir($dh)) !== false) {  
> if ($album!= “.” && $album!= “..” && $album!=“.DS\_Store”) {
> 
> ```
> echo "<?php
>         
> function $album(){ 
> 
> \$dirp = \"albums/$album/\";
> 
> ```
> 
> if (is\_dir($dirp)) {  
> if ($dhp = opendir($dirp)) {  
> while (($filep = readdir($dhp)) !== false) {  
> if ($filep!= "." && $filep!= ".." && $filep!=".DS\_Store") {
> 
> ```
> echo \"<td><img class='gallery' src='\" . \$dirp . \$filep . \"' /></td>\";
> }
> }
> closedir(\$dhp);
> }
> 
> ```
> 
> }
> 
> } ?\>
> 
> ";  
> }  
> }  
> closedir($dh);  
> }  
> }
> 
> ?\>
> 
> \<?php $dir = "albums/"; // Open a known directory, and proceed to read its contents if (is\_dir($dir)) { if ($dh = opendir($dir)) { while (($album = readdir($dh)) !== false) { if ($album!= "." && $album!= ".." && $album!=".DS\_Store") { echo "- $album 
> "; } } closedir($dh); } } ?\>
> 
> \<! This is the script where the gallery function physically runs --!\>
> 
> \<?php 
> 
> if (isset($\_GET[‘run’])) $linkchoice=$\_GET[‘run’];  
> else $linkchoice=‘’;
> 
> switch($linkchoice){
> 
> case ‘pioneering’ :  
> pioneering();  
> break;
> 
> case ‘second’ :  
> mySecond();  
> break;
> 
> default :  
> echo ‘no run’;
> 
> }
> 
> ?\>
> 
>   
> \<! End Script --!\>

---

<div class="post-metadata">

**Author:** ![sternbz](https://www.phphelp.com/letter_avatar_proxy/v4/letter/s/8c91f0/32.png) [@sternbz](https://www.phphelp.com/u/sternbz)\
**Post date:** [March 29, 2011, 10:05pm UTC](https://www.phphelp.com/t/auto-generating-filesystem-based-gallery/12989/2 "2011-03-29T22:05:12Z")

</div>

This section:

> \<?php 
> 
> if (isset($\_GET[‘run’])) $linkchoice=$\_GET[‘run’];  
> else $linkchoice=‘’;
> 
> switch($linkchoice){
> 
> case ‘pioneering’ :  
> pioneering();  
> break;
> 
> case ‘second’ :  
> mySecond();  
> break;
> 
> default :  
> echo ‘no run’;
> 
> }
> 
> ?\>

is actually incorrect… but the function recall will have the same type of generation script used above, just with the variables replaced to make everything match up.
