1

This is the code I am using for making a gallery type:

Main PHP :

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

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

$folder = opendir($folder_path);

if($num_files > 0)
{
    while(false !== ($file = readdir($folder))) 
    {
        $file_path = $folder_path.$file;
        $extension = strtolower(pathinfo($file ,PATHINFO_EXTENSION));
        if($extension=='jpg' || $extension =='png' || $extension == 'gif' || 

$extension == 'bmp') 
        {
            ?>
            <a href="<?php echo $file_path; ?>"><img src="<?php echo $file_path; ?>"  

height="200" /></a>
            <?php
        }
    }
}
else
{
    echo "the folder was empty !";
}
closedir($folder);

What should I do to display all image-names. Please help.

Blauharley
  • 4,186
  • 6
  • 28
  • 47
AnishDhoni
  • 37
  • 1
  • 7

2 Answers2

2

Try this inside while loop:

$file_path = $folder_path.$file;
$path_parts = pathinfo($file_path);
$extension = strtolower($path_parts['extension']);
if($extension=='jpg' || $extension =='png' || $extension == 'gif' || $extension == 'bmp')
{
    echo '<a href="'.$file_path.'"><img src="'.$file_path.'" height="200" />'.$path_parts['filename'].'</a>';
}
mith
  • 1,680
  • 1
  • 10
  • 12
1

Check this line:

$file_path = $folder_path.$file;

here $file is the name of the file, you can use this to display the file name like:

echo $file;

As per your requirement it is like:

<a href="<?php echo $file_path; ?>"><img src="<?php echo $file_path; ?>"
<?php echo $file; ?>
Mayank Pandeyz
  • 25,704
  • 4
  • 40
  • 59
  • Just another help how do i make it properly aligned with the image so that it is just below it not to its right or anywhere else.. – AnishDhoni Feb 23 '17 at 04:25
  • it will show the image name on next line. Use some css to align it where you want. – Mayank Pandeyz Feb 23 '17 at 04:27
  • so i was wondering whether u can help me with one more problem. i've included submit button below images.now i want that it should send that image to my email..can i do that??? – AnishDhoni Feb 23 '17 at 14:41
  • 1
    @AnishDhoni *"i've included submit button below images.now i want that it should send that image to my email..can i do that???"* - that's another question entirely; post a new question for it. – Funk Forty Niner Feb 23 '17 at 17:46