1

I'm able to display individual images by referring them using <img> tag but I have so many images so it would take lot of effort to refer every image individually.

Here is my php code

<?php
$folder_path = 'paintings/'; //image's     folder path

$num_files = glob($folder_path .     "*.{JPG,jpg,jpeg,gif,png,bmp}",     GLOB_BRACE);

$folder = opendir($folder_path);

if($num_files &gt; 0)
{
    while(false !== ($file =     readdir($folder))) 
{
        $file_path =     $folder_path.$file;
        $extension = strtolower(pathinfo($file ,PATHINFO_EXTENSION));
    if($extension=='jpg' || $extension =='png' || $extension == 'jpeg'|| $extension == 'gif' || $extension == 'bmp') 
    {
        ?&gt;
            &lt;a href="&lt;?php echo     $file_path; ?&gt;"&gt;&lt;img src="&lt;?php echo $file_path; ?&gt;"  height="250" /&gt;&lt;/a&gt;
            &lt;?php
    }
}
}
else
{
echo "the folder was empty !";
}
closedir($folder);
?>
6
  • So your question is...? (Please also note that glob() returns an array with all your filepaths, not the number of files) Commented Mar 7, 2016 at 12:30
  • Here I placed all images in a folder and display all images from that folder using php can you suggest any example.? Commented Mar 7, 2016 at 12:39
  • What is the problem with your code? It works. Commented Mar 7, 2016 at 12:40
  • I know code is perfect,but I just doesn't display images it display php ending tag and braces and semicolns and sometimes part of code. Commented Mar 7, 2016 at 12:44
  • OMG... Change all &gt; into > and all &lt; into <... (So all our edits was wrong! LOL) Commented Mar 7, 2016 at 12:47

1 Answer 1

2

As per comments, your code doesn't work because — for mysterious reason — you have changed all <> in &lt;&gt; (Maybe you have copied code from some web source?)

BTW, for the records: You use glob() to retrieve total file number and then readdir() to process each file.

You can directly process glob() result, improving performance of your code:

<?php
$folder_path = 'paintings/'; //image's     folder path
$files = glob($folder_path . "*.{JPG,jpg,jpeg,gif,png,bmp}", GLOB_BRACE);

if( count( $files ) )
{
    foreach( $files as $file_path )
    {
        ?>
            <a href="<?php echo $file_path; ?>"><img src="<?php echo $file_path; ?>"  height="250" /></a>
        <?php
    }
}
else
{
    echo "the folder was empty !";
}
?>

No need to check for extensions (with glob() you have only desired files).

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

Comments

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.