Skip to content
View in the app

A better way to browse. Learn more.

Web Designer Forum

A full-screen app on your home screen with push notifications, badges and more.

To install this app on iOS and iPadOS
  1. Tap the Share icon in Safari
  2. Scroll the menu and tap Add to Home Screen.
  3. Tap Add in the top-right corner.
To install this app on Android
  1. Tap the 3-dot menu (⋮) in the top-right corner of the browser.
  2. Tap Add to Home screen or Install app.
  3. Confirm by tapping Install.

PHP to echo out files as files

Featured Replies

Hi guys, a few years ago a very helpful chap on a forum helped me write this code which looks inside an image folder on my server and spits out images for my (what was used then lightbox2) but now is fancybox.

This is out of my league as I'm currently using codeAcademy to try to grasp what functions are in Javascript lol.

 

I was very close a minute ago as the images were coming up but I just put some more in there as paragraphs etc but now I have a problem. Saying error on line 116 but I can't see what. Tried adding ;

 

Structure:

images (contains images at main size) > thumbnails (contains same filenames as images but smaller thumbnails with same name as parent).

 

Want to write:

 

<div class="galleryItem">
        <a class="fancybox" rel="group" href="images/basement/concrete-basement.jpg"><img src="images/basement/thumbnails/concrete-basement.jpg" alt=""/></a>
</div>

My php is currently:

    <?php 
  
// settings 
$base = "images/basement";
$thumbs = "thumbnails"; 
  
$files = scandir($base); 
unset($files[0], $files[1]); 
unset($files[array_search('.DS_Store', $files)]); 
unset($files[array_search('thumbs', $files)]);

$chunk = array_chunk($files, 10);


// Define Paragraphs
$paragraph[1] = "<p>First Paragraph</p>";
$paragraph[2] = "<p>Second Paragraph</p>";


foreach($chunk as $i => $filearray)
{
    if ($paragraph[$i])
        echo "$paragraph[$i]";
    else if ($i > 0)
        echo "<p>Default Paragraph</p>";
    
    foreach ($filearray as $file)
    {
        echo '
		<div class=\"galleryItem\">
		
		\"<a class=\"fancybox\" rel=\"group\" href=\"images/$base/$file\"><img src=\"$base/$thumbs/$file\" alt=\"\"/></a>";	
    	
		</div>'
	}
}
?>

See anything guys?

Which is line 116?

 

The line below has the single and double quotes mixed up and some syntax errors:

 echo '
		<div class=\"galleryItem\">
		
		\"<a class=\"fancybox\" rel=\"group\" href=\"images/$base/$file\"><img src=\"$base/$thumbs/$file\" alt=\"\"/></a>";	
    	
		</div>'

try changing it to

echo "
		<div class=\"galleryItem\">
		
		<a class=\"fancybox\" rel=\"group\" href=\"images/$base/$file\"><img src=\"$base/$thumbs/$file\" alt=\"\"/></a>	
    	
		</div>";

Is $paragraph defined as an array earlier on? otherwise this line will fail

if ($paragraph[$i])

See if the above helps, otherwise could you be more specific about the error you're getting.

  • Author

Thanks gadgetgirl, sorry I'm not clear on making my points and I'm bad at code lol, so I'm not very good on both fronts lol. 116 is just a curly bracket (2nd one). I've got rid of that now, this is how I have my current code:

    <?php 
  
// settings 
$base = "images/basement";
$thumbs = "thumbnails"; 
  
$files = scandir($base); 
unset($files[0], $files[1]); 
unset($files[array_search('.DS_Store', $files)]); 
unset($files[array_search('thumbs', $files)]);

$chunk = array_chunk($files, 10);


// Define Paragraphs
$paragraph[1] = "<p>First Paragraph</p>";
$paragraph[2] = "<p>Second Paragraph</p>";


foreach($chunk as $i => $filearray)
{
    if ($paragraph[$i])
        echo "$paragraph[$i]";
    else if ($i > 0)
        echo "<p>Default Paragraph</p>";
    
    foreach ($filearray as $file)
    {
        echo '
		<div class="galleryItem">
		
		<a class="fancybox" rel="group" href="images/$base/$file"><img src="$base/$thumbs/$file" alt=""/></a>
    	
		</div>';
	}
}
?>

It currently echoes out first paragraph in text.

 

All code is as above. Sorry I'm a bit out of my depth lol. I can't wait till I understand this stuff; I know I have a long way to go though.

The reason you're not getting the error on line 116 anymore is because I fixed your code but you haven't sorted the single/double quotes. Single quotes displays everything literally, double quotes substitutes the value in place of the variable.

 

echo '$thumbs'; will output $thumbs

echo "$thumbs"; will output thumbnails

 

Use the code I supplied, run it in your browser and look at the source code. If the images don't appear its because your file structure is in error.

 

Do you really need the paragraphs because there's alot wrong with that bit of code. If you do need it, learn about arrays and offsets and how to access individual elements in an array.

  • Author

Thanks gadget, appreciate your help.

 

Changed your suggestion and had to make the following ammendment also:

 

previously; <a class=\"fancybox\" rel=\"group\" href=\"images/$base/$file\"><img src=\"$base/$thumbs/$file\" alt=\"\"/></a>";

 

<a class=\"fancybox\" rel=\"group\" href=\"$base/$file\"><img src=\"$base/$thumbs/$file\" alt=\"\"/></a>

 

Took me a while to understand ' " but I got it now. Big thanks for helping me grasp that.

Create an account or sign in to comment

Account

Navigation

Search

Search

Configure browser push notifications

Chrome (Android)
  1. Tap the lock icon next to the address bar.
  2. Tap Permissions → Notifications.
  3. Adjust your preference.
Chrome (Desktop)
  1. Click the padlock icon in the address bar.
  2. Select Site settings.
  3. Find Notifications and adjust your preference.