Get All the Images from a Folder in PHP

Pull all images from a specified directory and then display them

You can also use glob for this:

$dirname = "media/images/iconized/";
$images = glob($dirname."*.png");

foreach($images as $image) {
echo '<img src="'.$image.'" /><br />';
}

Listing all images in a directory using PHP

I like PHP's glob function.

foreach(glob(IMAGEPATH.'*') as $filename){
echo basename($filename) . "\n";
}

How to display images from a folder using php - PHP

You had a mistake on the statement below. Use . not ,

echo '<img src="', $dir, '/', $file, '" alt="', $file, $

to

echo '<img src="'. $dir. '/'. $file. '" alt="'. $file. $

and

echo 'Directory \'', $dir, '\' not found!';

to

echo 'Directory \''. $dir. '\' not found!';

how to display an image from a directory in PHP?

<?php
$dir="./img";
if(is_dir($dir)){
$files=scandir($dir);
unset($files[array_search('.',$files)]);
unset($files[array_search('..',$files)]);
$sn=0;
foreach($files as $key=>$val){
echo "SNo: ".++$sn."<br/>\n";
echo "Filename: ".$val."<br/>\n";
echo "Date-Time Modified: ".date('Y/m/d h:i',filemtime(rtrim($dir,'\/')."/".$val))."<br/>\n";
echo "Filesize: ".filesize(rtrim($dir,'\/')."/".$val)."bytes<br/>\n";
echo "<img src=\"".rtrim($dir,'\/')."/".$val."\" /><br/><br/>\n\n";
}
}else{
echo "(".$dir.") does not exist or is not a valid directory";
}
?>

PHP: Pull all images from a specified directory using relative image path

I would edit the loop in order to remove unnecessary string from path:

foreach($images as $image) {
$src = str_replace('/home/dev/public_html' ,'', $image) ;
echo '<img src="'.$src.'" /><br />';
}

How to download all images from a folder

You mean something like this? :

<?php
$files = glob("Image/"."*.*");
$countFiles = count($files); //Don't do calculations in your loop, this is really slow

for ($i=0; $i<$countFiles; $i++)
{
$num = $files[$i];

echo '<a href="download.php?file=' . $files[$i] . '"><img src="'.$num.'" alt="Here should be image" width="256" height="192" ></a><br/>';
}
?>

Getting images from subfolder and title of folder

This might help you. getDirContents will return all the folders with images.

function getDirContents($dir){
$files = scandir($dir);

foreach($files as $key => $value){
$path = realpath($dir.DIRECTORY_SEPARATOR.$value);
if(!is_dir($path)) {
$results[] = $value;
} else if($value != "." && $value != "..") {
$results[$value] = getDirContents($path);
}
}
return $results;
}

I have just provided the iteration of all directories, you need to add your div structure according to your need.

$directories = getDirContents('sub-directory'); // specify your sub-directory here as a parameter

if (!empty($directories)) {
foreach ($directories as $directory => $images) {
// this will be the parent folder of images
echo $directory;
if (!empty($images)) {
foreach ($images as $image) {
// add img tag here with valid url
echo $image;
}
}
}
}


Related Topics



Leave a reply



Submit