Jump to content

[SOLVED] how do i arrange my photos by their file names?


tiny

Recommended Posts

I have made a code that will retrieve all photos from a folder. It's working fine. The only problem is, the images are not arrange properly. My images are named this way:

gallery1.jpg

gallery2.jpg

gallery3.jpg

gallery4.jpg

gallery5jpg

gallery6.jpg

gallery7.jpg

gallery8.jpg

gallery9.jpg

gallery10.jpg

gallery11.jpg

gallery12.jpg

 

so when i have the images displayed, instead of being arranged that way, they appear in this arrangement:

 

gallery1.jpg

gallery10.jpg

gallery11.jpg

gallery12.jpg

gallery2.jpg

gallery3.jpg

gallery4.jpg

gallery5jpg

gallery6.jpg

gallery7.jpg

gallery8.jpg

gallery9.jpg

 

Here's the code:

<?php

$path =  "..".$_SERVER['REQUEST_URI']."images/stories/mygallery/thumbs/";
$dir_handle = @opendir($path) or die("Unable to open folder");

while (false !== ($file = readdir($dir_handle))) {
if(!in_array($file, array("index.php","..","."))){
	//explode to include only images
	$explodefile = explode(".", $file);
	if(in_array($explodefile[1], array("jpg","png","gif"))){
		$fileList[] = $file;
	}
}
}

closedir($dir_handle);

$i = 0;

while($fileList[$i] != ""){
  echo "<li><a href='http://localhost/mysite/galleryview'><img src='".$_SERVER['REQUEST_URI']."images/stories/mygallery/thumbs/{$fileList[$i]}'></a></li>";
  $i++;
}


?>

 

How can I arrange the images so they appear by their filenames accordingly (following their numbers) like this:

gallery1.jpg

gallery2.jpg

gallery3.jpg

gallery4.jpg

gallery5jpg

gallery6.jpg

gallery7.jpg

gallery8.jpg

gallery9.jpg

gallery10.jpg

gallery11.jpg

gallery12.jpg

 

Thank you in advance!

Link to comment
Share on other sites

Thank you for your reply. I read about natsort and tried to put it on my code: What I changed on the last part of my code was this:

 

$i = 0;

while($fileList[$i] != ""){
  $i++;
}

natsort($fileList);
print_r($fileList);

 

and the result was something like this:

Array ( [0] => gallery1.jpg [4] => gallery2.jpg [5] => gallery3.jpg [6] => gallery4.jpg [7] => gallery5.jpg [8] => gallery6.jpg [9] => gallery7.jpg [10] => gallery8.jpg [11] => gallery9.jpg [1] => gallery10.jpg [2] => gallery11.jpg [3] => gallery12.jpg )

 

it did arrange my images. but i'm not quite sure how i'm supposedly going to slice that result so that I can display each images like what i did on my previous code.

 

Link to comment
Share on other sites

Hi, I am still having trouble sorting my gallery. I tried to use natsort(); and I have this code:

 

$path = "/mypath";
			$images=array();
			$dh = @opendir($path) or die("Unable to open $path");
			echo "Directory Listing of $path<br/>";

			$i=0;

			while($f = readdir($dh))
			{
				if(is_dir($f))
				{
					continue;
				}
				else if($f != '.' && $f != '..')
				{
					//echo "<a href='$path/$file'>$file</a><br/>";
					$images[$i]=$f;
					$i++;
				}
			}
			natsort($images);
			for($i=0; $i<sizeof($images); $i++)
			{
			echo "<a href=".chr(34).$path.$images[$i].chr(34).">".$images[$i]."</a><br/>";

			}
closedir($dh);

 

the result still comes up this way..

gallery1.jpg

gallery10.jpg

gallery11.jpg

gallery12.jpg

gallery2.jpg

gallery3.jpg

gallery4.jpg

gallery5.jpg

gallery6.jpg

gallery7.jpg

gallery8.jpg

gallery9.jpg

 

what should i do? please help. :( thank you

Link to comment
Share on other sites

The array is in order, it's the way your outputting it. When natsort moves a value in the array it also moves the associated key. It makes more sense when you take into account the fact that PHP supports associative arrays. Once the array has been sorted the key is considered the name of that item in the array, rather than an actual index of it's actual numeric position.

 

I'm not sure if that made it any clearer or more confusing, but if you use this...

 

<?php
echo '<pre>';
print_r($images);
echo '</pre>';
?>

 

...you should see why your for loop is printing in the wrong order. As it happens the solution is very simple.

 

<?php
// replace
for($i=0; $i<sizeof($images); $i++) {
  echo "<a href=".chr(34).$path.$images[$i].chr(34).">".$images[$i]."</a><br/>";
}
// with something like
foreach($images as $image) {
  echo '<a href="' . $path . $image . '">' . $image . '</a><br/>';
}
?>

Link to comment
Share on other sites

glob() is less cluttering:

 

<?php
$path = '/mypath';
$images = glob($path . '/*.{jpg,jpeg,png,gif}', GLOB_BRACE);
natsort($images);
echo "Directory Listing of images in $path:<br />";
foreach ($images as $imagepath) {
$name = basename($imagepath);
echo "<br /><a href=\"$imagepath\">$name</a>";
}
?>

 

 

Link to comment
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.