I am just a beginner in php and have a PHP script (that i put together from snippets on the internet) that gets thumbnails and full-size images from folders. I want the images to sort by name, and they do but only on my local MAMP server and not on a server online. I am using the exact same php file for both.
For example, the order online of 3 images (from 12 total) is “View window”, “40 Trees”, “Orphans Walking”, as apposed to locally the sorting is “40 Trees, “Eva”, “For Caroline”, which is what I want.
<?php
$directory = 'images/slides/other/thumbnails';
$link = 'images/slides/other/';
$allowed_types=array('jpg','jpeg','gif','png');
$file_parts=array();
$ext='';
$title='';
$i=0;
$dir_handle = @opendir($directory) or die("There is an error with your image directory!");
while ($file = readdir($dir_handle))
{
if($file=='.' || $file == '..') continue;
$file_parts = explode('.',$file);
$ext = strtolower(array_pop($file_parts));
$title = implode('.',$file_parts);
if(in_array($ext,$allowed_types))
{
// Create a new row every four columns
if($i % 5 == 0 and $i != 0)
{
echo "</tr><tr>";
}
echo '<td align="middle" valign="middle"><a class="fancybox-button" rel="fancybox-button" href="'.$link.'/'.$file.'" title="'.$title.'">
<img src="'.$directory.'/'.$file.'"/>
</a>
</td>
';
$i++;
}
}
closedir($dir_handle);
?>
Can anyone help me out? Also, I would like to know if there is a simpler solution for getting the name of an image from the folder.
You need to pull in all the file names into an array, and then sort.
NOTE: rough example only; to make it better, carry the “explode” as part of the array to save doing it twice. Not tested directly, so you may need to clean up typos.