Readdir images from subdirectories and SORT alphabetically? - php

i found this code (https://stackoverflow.com/a/9628457/1510766) for show all images from directories and sub-directories, and works fine, but im trying to implement the sort() function of php but doesnt work:
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);
// -- SORTING the FILES --
sort($files);
return $files;
}
}
foreach (ListFiles('works/'.$service_get_var.'/') as $key=>$file){
echo "<li><img src=\"$file\"/></li>";
}
When I test this, I cant see any images, is the correct use for sort()?. Thank you very much.

Sort after read all, not in recursive steps
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);
// -- SORTING the FILES --
//sort($files);
return $files;
}
}
$list = ListFiles('works/'.$service_get_var.'/');
sort($list);
foreach ($list as $key=>$file){
echo "<li><img src=\"$file\"/></li>";
}

Related

PHP - Remove '.' and '..' from values fetched from directory files

I am using this code in order to get a list files from directory:
$dir = '/restosnapp_cms/images/';
if ($dp = opendir($_SERVER['DOCUMENT_ROOT'] . $dir)) {
$files = array();
while (($file = readdir($dp)) !== false) {
if (!is_dir($dir . $file)) {
$files[] = $file;
}
}
closedir($dp);
} else {
exit('Directory not opened.');
}
I want to get rid of the values '.' and '..'.
Is it possible to do this? Thank you. :)
Just check for them first:
while ($file = readdir($p)) {
if ($file == '.' || $file == '..') {
continue;
}
// rest of your code
}
DirectoryIterator is much more fun than *dir functions:
$dir = new DirectoryIterator($_SERVER['DOCUMENT_ROOT'] . $dir);
foreach($dir as $file) {
if (!$file->isDir() && !$file->isDot()) {
$files[] = $file->getPathname();
}
}
But the bottomline is regardless of which way you do it, you need to use a conditional.

opening two folders and displaying images php

I've got the problem of trying to display two albums of photos from a db using php. Currently the following code works but for just one album. Basically I need it to display photos from 'mount-everest-part-2' aswell.
<?php
$path = "images/galleries/";
$album = 'mount-everest-part-1';
if ($handle = opendir($path.$album.'/thumbs/')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." && substr($file, 0, 2) != '._') {
$files[] = $file;
}
}
closedir($handle);
}
asort($files);
foreach($files as $file) {
echo '<li><img src="../' . $path . $album . '/thumbs/' . $file . '" /></li>';
}
?>
How can I use this code to open two files and spit the files out using the same foreach loop?
This sounds like one of those things that OOP would suit nicely. Here's an example:
<?php
class Album_Picture_File {
private $fileName;
private $path;
private $album;
public function __construct($fileName, $path, $album) {
$this->fileName = $fileName;
$this->path = $path;
$this->album = $album;
}
private function getAlbumPath() {
return '../' . $this->path . $this->album;
}
public function getPicturePath() {
return $this->getAlbumPath() . '/images/' . $this->fileName;
}
public function getThumbnailPath() {
return $this->getAlbumPath() . '/thumbs/' . $this->fileName;
}
}
function fetchFiles($path, $album) {
$files = array();
if ($handle = opendir($path.$album.'/thumbs/')) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." && substr($file, 0, 2) != '._') {
$fullPath = $path . $album . '/thumbs/' . $file;
$files[$fullPath] = new Album_Picture_File($file, $path, $album);
}
}
closedir($handle);
}
ksort($files); //sort after key (out file path)
return $files;
}
$files = array_merge(
fetchFiles('images/galleries/', 'mount-everest-part-1'),
fetchFiles('images/galleries/', 'mount-everest-part-2')
);
foreach($files as $file) {
echo '<li><img src="' . $file->getThumbnailPath() . '" /></li>';
}
?>
Note that instead of pushing $files with strings, we push it with Album_Picture_File objects.
Create a global array :
$all = array();
Then, make 2 loops and push the global array (create a function that reads the directory for example)
$files_dir_one = getFiles("your_dir");
$files_dir_two = getFiles("your_dir2");
$all = array_merge($files_dir_one, $files_dir_two);
function getFiles($directory) {
$files = array();
if ($handle = opendir($directory)) {
while (false !== ($file = readdir($handle))) {
if ($file != "." && $file != ".." && substr($file, 0, 2) != '._') {
$files[] = $file;
}
}
closedir($handle);
}
//asort($files);
return $files;
}
And then, the last step : populate the view
How can I use this code to open two files and spit the files out using the same foreach loop?
Taking your question literally it works like this. First of all extract a function with the two input variables:
/**
* #param string $path
* #param string $album
*/
function list_images($path, $album) {
$files = [];
if ($handle = opendir($path . $album . '/thumbs/'))
{
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != ".." && substr($file, 0, 2) != '._')
{
$files[] = $file;
}
}
closedir($handle);
}
asort($files);
foreach ($files as $file)
{
echo '<li><img src="../' . $path . $album . '/thumbs/' . $file . '" /></li>';
}
}
Then you can just iterate over your two albums and output both:
$path = "images/galleries/";
$albums = ['mount-everest-part-1', 'mount-everest-part-2'];
foreach ($albums as $album)
{
list_images($path, $album);
}

I want to show images in the directory online but it not show the images properly how I can do this

I am trying to show images from online directory but images not shown properly and breaks what I can do to show these all images properly
<?php
// Connects to your Database
include_once("connection.php");
//read image directory
$images_dir = '/home/qeplaho/public_html/image/';
function ListFiles($images_dir) {
if($dh = opendir($images_dir)) {
$files = Array();
$inner_files = Array();
while($file = readdir($dh)) {
if($file != "." && $file != ".." && $file[0] != '.') {
if(is_dir($images_dir . "/" . $file)) {
$inner_files = ListFiles($images_dir . "/" . $file);
if(is_array($inner_files)) $files = array_merge($files, $inner_files);
} else {
array_push($files, $images_dir . "/" . $file);
}
}
}
closedir($dh);`enter code here`
return $files;
}
}
echo "<table>";
foreach (ListFiles('/home/qeplaho/www/image/') as $key=>$file){
echo "<tr>";
echo"<td>";
echo '<img src=\"$images_dir\" width="200" height="200"/>';
echo "</td>";
echo "</tr>";
}
echo "</table>";`<code>
this should do the trick:
$handle = opendir(dirname(realpath(__FILE__)).'/pictures/');
while($file = readdir($handle)){
if($file !== '.' && $file !== '..'){
echo '<img src="pictures/'.$file.'" border="0" />';

How to display all my files by date modified?

I'm new to PHP and i was just wondering if someone could help. I want my code to read files from a directory/sub directory and display all of them by the date they were modified! My code displays only one file, which is the one that I recently changed. So how do I list all the files? I hope this question makes some sense..
<?php
$last_mtimes = array();
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);
$lmtime = filemtime($dir . "/" . $file) ;
$last_mtimes[$lmtime] = $dir . "/" . $file;
}
}
}
krsort($last_mtimes);
closedir($dh);
return ($last_mtimes);
}
}
foreach (ListFiles('folder/folder/') as $key=>$file);
echo array_shift(ListFiles('folder/folder/'));
?>
There is an extra semi colon which makes the loop do nothing:
foreach (ListFiles('folder/folder/') as $key=>$file);
^ remove this

Pull all images from multiple directories and display them with PHP

I have a folder on my server called 'images', and within that folder I could have a single folder to as many as 10 folders that contain images.
Instead of writing a tag for each image
<img src="images/people/001.jpg">
<img src="images/landscape/001.jpg">
etc etc
Can I use PHP to get all the images in all the folders in the main directory 'images'?
I have VERY little experience with PHP, so this is something I am struggling with.
I need php to return an array of '<div class="box"><img src="images/FOLDER/IMAGENAME.jpg"></div>'
Maybe someone can help.
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?
A simpler soluton. You can use built-in glob function. Assuming that all of your images are .jpg:
$result = array();
$dir = 'images/';
foreach(glob($dir.'*.jpg') as $filename) {
$result[] = "<div class=\"box\"><img src=\"$filename\"></div>";
}
Then you can echo each element of $result or whatever you want.

Categories