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();

?>

 

Link to comment
Share on other sites

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();
?>

Link to comment
Share on other sites

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();

 

 

?>

Link to comment
Share on other sites

<?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();


?>

Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.