How do I sort pictures alphabetically in gallery? - php

I have this php gallery and I need to sort pictures alphabetically. How can I do that? Thank you.
<?php
$dir = htmlspecialchars($_GET["dir"]);
$className = htmlspecialchars($_GET["className"]);
if ($handle = opendir($dir)) {
echo "<div class='photogallery $className'><ul>";
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != ".." && preg_match('/(jpg)|(JPG)$/', $entry) == 1) {
$path = $dir."/".$entry;
$pathThumb = $dir."/thumbs/".$entry;
echo "<li><a href='/$path' data-lightbox='gallery1' data-title=''><img src='/$pathThumb' alt=''></a>
</li>";
}
}
closedir($handle);
echo "</ul><div class='both'></div></div>";
}
?>

glob returns the directory sorted unless you specify otherwise.
echo "<div class='photogallery $className'><ul>";
foreach (glob($dir . "/*.*") as $entry) {
if ($entry != "." && $entry != ".." && preg_match('/(jpg)|(JPG)$/', $entry) == 1) {
$path = $dir . "/" . $entry;
$pathThumb = $dir . "/thumbs/" . $entry;
echo "<li><a href='/$path' data-lightbox='gallery1' data-title=''><img src='/$pathThumb' alt=''></a>
</li>";
}
}
echo "</ul><div class='both'></div></div>";

Related

List all folders in directory (PHP) [duplicate]

This question already has answers here:
PHP Get all subdirectories of a given directory
(16 answers)
Closed 5 years ago.
How can I make my code only display links to the folders and not the files in the directory?
$d = dir(".");
echo "<ul>";
while(false !== ($entry = $d->read())) {
echo "<li><a href='{$entry}'>{$entry}</a></li>";
}
echo "</ul>";
$d->close();
$d = dir(".");
echo "<ul>";
while (false !== ($entry = $d->read()))
{
if (is_dir($entry) && ($entry != '.') && ($entry != '..'))
echo "<li><a href='{$entry}'>{$entry}</a></li>";
}
echo "</ul>";
$d->close();
You should just be able to wrap your current code with a call to is_dir:
while(false !== ($entry = $d->read())) {
if (is_dir($entry)) {
echo "<li><a href='{$entry}'>{$entry}</a></li>";
}
}
If you want to remove the "dot" directories (. and ..), use the following:
if (is_dir($entry) && !in_array($entry, ['.', '..'])) {
...
Just check if the $entry is a directory:
$d = dir(".");
echo "<ul>";
while(false !== ($entry = $d->read())) {
if(is_dir($entry))
echo "<li><a href='{$entry}'>{$entry}</a></li>";
}
echo "</ul>";
$d->close();

Insert custom CSS class into function?

I want to use my custom CSS style on that code, any ideas how it should be done?
//Load modules
if ($handle = opendir('modules'))
{
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != "..")
{
$replace = preg_replace( array ('/(.*?)/', '/[-]/', '/.php/'), array ('$1', ' '), $file);
$result = ucfirst($replace);
echo '<a href="?inav=';
echo $replace;
echo '">';
echo $result;
echo '</a><br>';
}
}
closedir($handle);
And that I want to insert:
<a href="?inav=" class="button101"><span class="user icon">
Just add the echo you need:
//Load modules
if ($handle = opendir('modules'))
{
while (false !== ($file = readdir($handle)))
{
if ($file != "." && $file != "..")
{
$replace = preg_replace( array ('/(.*?)/', '/[-]/', '/.php/'), array ('$1', ' '), $file);
$result = ucfirst($replace);
echo '<a href="?inav=';
echo $replace;
echo '"';
echo ' class="button101"';
echo '>';
echo $result;
echo '</a><br>';
}
}
closedir($handle);

how can i add child node to the parents nodes in php?

I am reading a rootpath and listing all the folder. I have to add some child node to every folder programatically ?
<?php
$rootpath = 'D:/Storage/';
if ($handle = opendir($rootpath)) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
echo "<li><a href=/hi.php?dev=$entry&action=viewcam>$entry</a>";
}
}
closedir($handle);
?>
I am getting like
- folder1
- folder2
but I want like
- folder1
Appple
orange
- folder2
Appple
orange
Any suggestion ?
There is no need to specifically "create a node" on the server side for this. All you have to do is output the html markup you want to have:
<?php
$rootpath = 'D:/Storage/';
if ($handle = opendir($rootpath)) {
echo "<ul>\n";
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != "..") {
echo "<li>$entry</li>\n";
echo "<ul>\n";
foreach (['Apple', 'orange'] as $fruit) {
echo "<li>".$fruit."</li>\n";
}
echo "</ul>\n";
}
}
echo "</ul>\n";
}
closedir($handle);
?>
(I fixed a few minor issues with your code on-the-fly...)
Obviously this is just an example to show the basic approach.

How to change the style of a few echoed pieces of text?

<?php
$blacklist = array("no.html", "one.php");
if ($handle = opendir('.')) {
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != ".." && !in_array($entry, $blacklist)) {
echo "<a href='$entry'><li id='file'>$entry\n</li></a>";
}
}
closedir($handle);
}
?>
What happens here is that every file in the directory (excluding those in the blacklist) get echoed out like so:
1.html
2.html
3.html
4.html
5.html
6.html
7.html
8.html
However, is there any way to style certain elements? For example, every 3 echo's, make the text bigger and the color red.
E.g.
1.html
2.html
3.html
4.html
5.html
6.html
7.html
8.html
Try this. I think it will help you
$i = 1;
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != ".." && !in_array($entry, $blacklist)) {
if($i%3 == 0){
echo "<a href='$entry'><strong><li id='file'>$entry</li></strong></a>";
}else{
echo "<a href='$entry'><li id='file'>$entry</li></a>";
}
}
$i++;
}
Something like this with the counter initialized will do the job.
while (false !== ($entry = readdir($handle))) {
if ($entry != "." && $entry != ".." && !in_array($entry, $blacklist)) {
if ($i%3==0)
{
//the echo with the style
}
else
{
echo "<a href='$entry'><li id='file'>$entry\n</li></a>";
}
}
$i++
}

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

Categories