Jump to content

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

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • 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.