Pull All Images from a Specified Directory and Then Display Them

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 />';
}

Pull all images from multiple directories and display them with PHP

function ListFiles($dir) {
if($dh = opendir($dir)) {
$files = Array();
$inner_files = Array();
while($file = readdir($dh)) {
if($file != "." && $file != ".." && $file[0] != '.') {
if(is_dir($dir . "/" . $file)) {
$inner_files = ListFiles($dir . "/" . $file);
if(is_array($inner_files)) $files = array_merge($files, $inner_files);
} else {
array_push($files, $dir . "/" . $file);
}
}
}
closedir($dh);
return $files;
}
}
foreach (ListFiles('/path/to/images') as $key=>$file){
echo "<div class=\"box\"><img src=\"$file\"/></div>";
}

Something like this?

I need to retrieve ALL images (regardless of number and name) from a local folder and display them on my website

The basic idea is solid, there are just some details in the execution that need to be cleaned up. I took a swing at setting this up locally and cleaning it up a bit.

PHP:

<?php
/**
* This PHP script returns javascript that feeds an image gallery. A JSON array of images are returned along with a
* parameter that indicated the base URL for the images.
*
*/

/**
* @var $localImageDir string
* The path to the local image directory relative to this script
*/
$localImageDir = 'img';

/**
* @var $baseUrl string
* The URL path to the image directory
*/
$baseUrl = 'http://localhost/img/';

/**
* This function returns an array of image file names in the specified directory. The output is limited
* using the limit parameter
*
* @param string $localImageDir
* @param string $baseUrl
* @param int $limit
* @return array
*/
function returnImages($localImageDir = '.', $baseUrl='', $limit = 5)
{
//valid image extensions
$pattern = "/(\.jpg$)|(\.png$)|(\.jpeg$)|(\.gif$)/";

$files = array();
if ($handle = opendir($localImageDir))
{
while (false !== ($file = readdir($handle)))
{
//Use preg_match here, eregi is deprecated
if (preg_match($pattern, $file))
{
$files[] = $baseUrl.$file;
}
}

closedir($handle);
}

// Limit results
$output = array_slice($files, 0, $limit);

return $output;
}

// Get the image list as an array
$imageArray = returnImages($localImageDir, $baseUrl);

//Encode the image array as JSON
$json = json_encode($imageArray);

//Set the appropriate HTTP header and echo the JavaScript
Header("content-type: application/x-javascript");
echo 'var galleryImages=' . $json.';';

HTML:

<!DOCTYPE html>
<html lang="en">
<head>
<script src="images.php"></script>
<script type="text/javascript">
var curImg=0;
function rotateImages()
{
document.getElementById("slideshow").setAttribute("src", galleryImages[curImg]);
curImg=(curImg<galleryImages.length-1)? curImg+1 : 0;
}

window.onload=function()
{
setInterval("rotateImages()", 2500)
}
</script>
</head>
<body>
<div style="width: 170px; height: 160px">
<img id="slideshow" src="pics/bear.gif"/>
</div>
</body>
</html>

In your original script, there were some syntax errors in your for loop - those "i"s need $s. Also, $files(i) should be $files[i]. Beyond that, you're doing a lot of work that you don't need to do, you can simply create an array of file names and use json_encode to format them properly for JavaScript. It's also a good idea to have the base URL to your images set up as a variable in this script rather than in the client. We can send that along with the image data.

You're also using eregi, which is deprecated, I replaced that with preg_replace, which requires a small tweak to your regex.

On the HTML side, I just added correct barebones markup, and changed function and variable names to be a bit more readable, and changed the script to use the variable base URL.

You will need to change the path to the PHP script in the HTML, I just changed it for my local setup.

Good luck.

simple PHP, echo all images from a directory within li.../li

You have too many closing </li> tags :

<?php
$files = glob("images/*.*");

for ($i=0; $i<count($files); $i++) {
$image = $files[$i];
echo '<li class="col-lg-2 col-md-2 col-sm-3 col-xs-4"><img class="img-responsive" src="'.$image .'" /></li>';
}

?>

And you don't need the print $image ."<br />";

Pull all images from a URL folder and display in Boostrap HTML

Bit late but I figured I'd answer this. The below PHP code loads all ".png" images from the directory and then echos the image tag. You would replace the plain html tag for the equivalent bootstrap one.

dirname = "media/images/cats/";
$images = glob($dirname."*.png");
foreach($images as $image) {
echo '<img src="'.$image.'" /><br />';
}


Related Topics



Leave a reply



Submit