Jump to content

I can read a dir of images and display the file names. How can I view the images


vetman

Recommended Posts

I can read a dir of images and display the file names. How can I view the images? Any help would be appreciated. I am new to this!

This is my code:

 

<?php

//Open images directory

$dir = dir("../images");

 

//List files in images directory

while (($file = $dir->read()) !== false)

{

echo "filename: " . $file . "<br />";

}

 

$dir->close();

?>

 

Just use <img src=... as usual in for HTML image displays. Your code should look like this:

 

<?php
//Open images directory
$dir = dir("../images");

//List files in images directory
while (($file = $dir->read()) !== false)
{
echo '<img src="$file" alt="my image" width="" height="" />';
";
}

$dir->close();
?>

Okay, I got myself into another bind. I want to put 3 files in a row across the page, then 3 in the next line and so forth.

 

This is the modified code below:

 

<?php

//Open images directory

$dir = dir("../images");

 

//List files in images directory

while (($file = $dir->read()) !== false)

{

// how many entries per row

$per_row = 3;

 

echo '

 

<table border=1>';

 

$count = 0;

echo '<img src="../images/' . $file . '" alt="my image" width="" height="" />';

for($x = 0; $x < count($file); $x++)

{

    if($count == 0)

    {

        echo '

        <tr>';

    }

 

    echo '

        <td>'.$file[$x].'</td>';

 

    $count++;

 

    if(($count == $per_row) || ($file[$x] == end($file)))

    {

        echo '

    </tr>';

        $count = 0;

    }

}

 

echo '

</table>';

}

 

$dir->close();

 

 

?>

<?php
//Open images directory
$dir = dir("../images");

// this shouldn't be in the while loop
$per_row = 3;

echo '
<table>
	<tr>';

$i = 0;

// create the rows
while (($file = $dir->read()) !== false) {
$i++;

// close and start another row if the correct number has been displayed
if ($i % $per_row == 0) {
	echo '
	</tr>
	<tr>';
}

echo '
		<td><img src="../images/' . $file . '" alt="my image" /></td>';

}

// finish the current row
while ($i % $per_row != 0) {
echo '
		<td> </td>';
}

echo '
	</tr>
</table>';
}

$dir->close();


?>

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.