Jump to content

Align by name


Regoch2

Recommended Posts

I find this script online for geting images from directory. Is there any way to change code to get it align by name? With this code I get random align!

Thanks!

<?php
/* Configuration Start */
$orig_directory = 'images/galeria';
/* Configuration end */

$allowed_types=array('jpg','jpeg','gif','png');
$file_parts=array();
$ext='';
$title='';
$i=0;

/* Opening the image directory and looping through all the thumbs: */
$dir_handle = @opendir($orig_directory) or die("There is an error with your image directory!");
$i=1;
while ($file = readdir($dir_handle)) 
{
/* Skipping the system files: */
if($file=='.' || $file == '..') continue;
$file_parts = explode('.',$file);
$ext = strtolower(array_pop($file_parts));

/* Using the file name (withouth the extension) as a image title: */
$title = implode('.',$file_parts);
$title = htmlspecialchars($title);

/* If the file extension is allowed: */	
if(in_array($ext,$allowed_types))
{
	/* Outputting each image: */
	echo '<img src="/'.$orig_directory.'/'.$file.'" width="893" height="597" /> ';
}
}

/* Closing the directory */
closedir($dir_handle);
?>

Link to comment
https://forums.phpfreaks.com/topic/268356-align-by-name/
Share on other sites

This is what I get working! Thanks spiderwell for direction!

<?php
$folder = 'images/galeria'; // Choose directory
$extensions = array('jpg','jpeg','gif','png');
$slika = scandir($folder);
sort($slika); // Sort the array
foreach($slika as $key => $value) {       
	$get_extensions = explode(".",$value);
        $ext = $get_extensions[count($get_extensions) - 1];
         // Only include files with desired extensions
        if (in_array($ext, $extensions))
        {
    $title = substr($value, 0,strrpos($value,'.')); // Get filename for alt
	echo '<img src="/'.$folder.'/'.$value.'" width="893" height="597" alt="'.$title.'" /> '; 
        }
}
?>

Link to comment
https://forums.phpfreaks.com/topic/268356-align-by-name/#findComment-1377858
Share on other sites

Archived

This topic is now archived and is closed to further replies.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.