0

I am retrieving images from a directory using php. Images are displayed on PHP page (getUser.php) but how can these images be display on any HTML.
getUser.php

<?php


$dir = 'images';
$file_display = array('jpg','jpeg','png','gif');

if (file_exists ($dir) == false) {
    echo 'Directory \'', $dir, '\' not found!';
}
else{
    $dir_contents = scandir($dir);


    foreach($dir_contents as $file) {
        $file_type = strtolower(end(explode('.', $file)));

        If($file !== '.' && $file !== '..' && in_array($file_type, $file_display) == true) {
            echo '<img src="', $dir, '/', $file, '" alt="', $file, '" />';
            /*echo '<td>';
echo '<img src =';
echo $dir."/".$file;
echo  '/>';
echo '</td>'; */

        }       

    }

}


?> 

one.html
I want this array of images to be displayed in html (div). That is the source of the image tag takes reading from the PHP file. I have done this in ASP.NET but can't figure it out in PHP Is there anything like

<img src="images/vimages/<%=row["image"] %>" ASP.NET

this PHP.

<div id="pics">
                <img src="img/...jpg" alt=""  />

</div>


The above div will be dynamically populating using getUser.php file.

3
  • So what doesn't work? What means on any html? Commented May 5, 2012 at 10:24
  • @DenisErmolin The images in the array are display via echo. But i want them to be display by <img> tag i.e. in HTML page. How i am going to do that Commented May 5, 2012 at 10:27
  • I would suggest you to reformulate your question. It was hard to understand what do you want. And it is not really related to images, so I would change the title. Commented May 5, 2012 at 15:44

2 Answers 2

1

If I understand your question, you are looking for some template system in php.

Firstly, there is one already in the php. PHP allows you to mix html and php code using tags.

So you can create something like:

<?php if ($images): ?>
<?php $counter = 1 ?>
<ul>
<?php foreach ($images as $image): ?>
    <li id="item-<?php echo $counter++ ?>">
    <img src="<?php echo $image['source']?>">
    </li>
<?php endforeach ?>
</ul>
<?php endif?>

While it is good practice to separate application logic and html code, pure php is not the best tool for this job. Fortunately, there is plenty of tools on web, for example Smarty or Latte

Sign up to request clarification or add additional context in comments.

Comments

0

Consider the following:

ASP.NET

<img src="images/vimages/<%=row["image"] %>">

PHP

<img src="images/vimages/<?=$row["image"]?>">

1 Comment

However, then must be short_open_tag enabled. Read more at php.net/manual/en/function.echo.php

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.