Post: [PHP]Tutorial: Reading files from a folder
05-10-2011, 02:15 AM #1
(adsbygoogle = window.adsbygoogle || []).push({});
Originally posted by 039

[align=center][size=large][b]Reading files from a folder and putting them on a page.[/b][/size][/align]

Intro
This is generally pretty simple but still useful, for things like commercial websites where you have to add and delete products.
You can use this method to read it from a database which I originally intended to write a tutorial for but currently I'm having some issues setting up MySQL on my laptop so I'll do something like that later.

Anyway, let's start by writing the basic layout for our page. In my case I'll just display a table of images so if that's all you need you can simply copy the following code.

Css
    <style type="text/css">

.collumn
{
border-style: solid;
border-width: 2;
border-color: black;
}

.image
{
width: 100;
height: 100;
}

</style>


HTML
    
<html>
<head>
<title>Awesome faceisplay</title>
<table>
<tr>
</table>

</body>

</html>


Code
Now let's start with our php code, first declare two variables:
    $fPath = basename('/Images/'Winky Winky;
$count = -1;


The first one is the path to the folder we're going to read, basename changes the relative path into an absolute path meaning it will get the full link to the directory's location. Make sure the image folder is in the same directory as your php page.
count we'll be using to count the amount collumns we'll need starting with -1 otherwise our first row will only have 3 images.

Next we'll write an "if" statement to check if the directory can be opened and immediately create a new variable which contains the directory as an object, this also means the directory is opened.

    if ($handle = opendir($fPath)) 
{

}


Between these lines we'll write a while loop.

    while (false !== ($file = readdir($handle))) 
{

}


Everything between these lines will loop until there are no more files to be read in the folder.

Next we'll make sure our code doesn't add the "." and ".." as an image and then choose the amount of our collumns.

    if ($file != "." && $file != "..") 
{
if ($count == 3)
{
$count = 0;

}
else
{
$count++;
}

}


Now we'll print the html code we need using php. Under count = 0 add the this line
    print "<tr/>\n<tr>\n";


Change the ($count == 3) to the number of collumns you need -1.

and under the else statement add this code:

    print "<td class='collumn'>";
print "<img class='image' src='$fPath/$file' />";
print "</td>\n";


Finally close the directory:
    closedir($handle);


To make sure the tablerow is closed we'll add this final piece of code:
    if ($count !== 0)
{
print "<tr/>";
}


Result:
That's it, this is the result you should have depending on the images in the folder you're using:

You must login or register to view this content.

Full code:
    
<html>
<head>
<title>Awesome faceisplay</title>

<style type="text/css">

.collumn
{
border-style: solid;
border-width: 2;
border-color: black;
}

.image
{
width: 100;
height: 100;
}

</style>

</head>
<body>

<table>
<tr>
<?php
$fPath = basename('/Images/'Winky Winky;
$count = -1;

if ($handle = opendir($fPath))
{
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != "..")
{
if ($count == 3)
{
$count = 0;
print "<tr/>\n<tr>\n";
}
else
{
$count++;
}
print "<td class='collumn'>";
print "<img class='image' src='$fPath/$file' />";
print "</td>\n";
}
}
}
closedir($handle);

if ($count !== 0)
{
print "<tr/>";
}

?>
</table>

</body>

</html>



source You must login or register to view this content.
(adsbygoogle = window.adsbygoogle || []).push({});

Copyright © 2026, NextGenUpdate.
All Rights Reserved.

Gray NextGenUpdate Logo