Page 1 of 1

Read dir contents into an array - returns wrong dir

Posted: Sat Nov 27, 2004 6:21 pm
by raging radish
I'm trying to do a simple script which will take the contents (all .jpg files) of a directory and return a thumbnailed table. Everthing after the "while" loop works just fine. Near as I can tell, the directory reading works as well except it always returns the contents of the current directory rather than the contents of the specified directory, being img_1. I've obviously missed something of vital significance but I can't see what it is and various searches don't really address my issue.

The code (don't laugh)

Code: Select all

<?php
$pic_dir = opendir('img_1');
	while (false !== ($fotos[] = readdir($pic_dir))) {
  if ($fotos != "." && $fotos != "..")

		while (count($fotos)) {
			$val=array_shift($fotos);
			print "<table cellpadding='2' cellspacing='2'>";
			print "<tr><td><a href=".$val."><IMG src=".$val." width="170" height="127"></a></td>";
			$val=array_shift($fotos);
			print "<td><a href=".$val."><IMG src=".$val." width="170" height="127"></a></td>";
			$val=array_shift($fotos);
			print "<td><a href=".$val."><IMG src=".$val." width="170" height="127"></a></td></tr>";
			print "</table>";
			}
}
?>[/b]

Posted: Sat Nov 27, 2004 6:38 pm
by rehfeld
well, its reading from the correct directory.

your problem is that you are outputting the filnames directly. when you use readdir(), it just returns the filenames, it does not add the directory name to it, which you must do manuall in your case

Code: Select all

$dir = 'img_1';

$dh = opendir($dir);

while (false !== ($file = readdir($dh))) {
    if ($file&#1111;0] !== '.') {
        echo "$dir/$file";
    }
}
btw notice how i checked the files before echoing them.
you may or may not want to stick w/ your method, but heres the diff

the way i did it checks if the first character of the filename is '.'

if you had some other files in that directory, maybe .htaccess and .htpassword, it would not echo their names out, because the first char in thier filename is '.'
and it will of course also exclude '.' and '..' as well, and anything else that starts w/ '.'

that may or may not be the desired behavior for what your doing, just thought you might like a tip

Posted: Sat Nov 27, 2004 8:26 pm
by raging radish
Thanks for the nudge in the right direction. I didn't really understand how I had to "manually add the directory" but the line --> echo "$dir/$file"; was a valuable clue.

The amended code:

Code: Select all

<?php
$pic_dir = 'img_1';

$dh = opendir($pic_dir);

while (false !== ($file = readdir($dh))) {
    if ($file[0] !== '.') {
//   echo "$pic_dir/$file";
    $fotos[]="$pic_dir/$file";
    }
}
		while (count($fotos)) {
			$val=array_shift($fotos);
			print "<table cellpadding='2' cellspacing='2'>";
			print "<tr><td><a href=".$val."><IMG src=".$val." width="170" height="127"></a></td>";
			$val=array_shift($fotos);
			print "<td><a href=".$val."><IMG src=".$val." width="170" height="127"></a></td>";
			$val=array_shift($fotos);
			print "<td><a href=".$val."><IMG src=".$val." width="170" height="127"></a></td></tr>";
			print "</table>";
			}
?>
The directory(ies) is strictly for images so there should not be any files in there with a preceding ".", but it was my understanding that the line:

Code: Select all

if ($fotos != "." && $fotos != "..")
would stop the incusion of "." and "..". I assume this is still technically correct, assuming files such as .htaccess are not a factor.

Posted: Sat Nov 27, 2004 8:53 pm
by rehfeld
yup that correct.

i just prefer to exclude all files which begin w/ a .

i havent yet encountered a time when i would actually want to list files which may begin w/ a dot, but i have later added htaccess files to directorys that previously did not have one, so i just kinda err on the safe side and make sure that if i did one day add a file that begins w/ a . , that my code wouldnt accidently list it.

for example now i almost always have an .htaccess file in my images folders, so i can set how long i want browsers to cache my images.

Posted: Sun Nov 28, 2004 1:27 pm
by raging radish
I realized last night that there was a serious limitation with the original script since it downloads the full size images and resizes them to the specified dimensions - a directory containing 40 pics averaging 150k in size translates to a 6M download before the page fully loads. Not good for modem users, or anybody else for that matter. So I added a few bits, the idea being that the top directory contains thumbnailed images and a directory within the thumbnail dir contains the full size images. The result is the following:

Code: Select all

<?php

$thumb_dir = 'thumbnail_dir';
$dh = opendir($thumb_dir);
$lg_dir='thumbnail_dir/large_pics';
$dh_lg=opendir($lg_dir);

while (false !== ($file = readdir($dh))) {
	if ($file [0]!= "." && $file [0] != ".." && ! is_file("$file.jpg")) {
	$fotos[]="$pic_dir/$file";
    }
}
while (false !== ($file_lg = readdir($dh_lg))) {
	if ($file_lg [0] != "." && $file_lg [0] != ".." && ! is_file("$file_lg.jpg")) {
	$fotos_lg[]="$lg_dir/$file_lg";
    }
}

	while (count($fotos)) {
		sort($fotos);
		sort($fotos_lg);
		$val=array_shift($fotos);
		$val_lg=array_shift($fotos_lg);
		print "<table cellpadding='2' cellspacing='2'>";
		print "<tr><td><a href=".$val_lg."><IMG src=".$val."></a></td>";
		$val=array_shift($fotos);
		$val_lg=array_shift($fotos_lg);
		print "<td><a href=".$val_lg."><IMG src=".$val."></a></td>";
		$val=array_shift($fotos);
		$val_lg=array_shift($fotos_lg);
		print "<td><a href=".$val_lg."><IMG src=".$val."></tr>";
		print "</table>";
		}
?>
A had a bit of an issue with the directories not being "synchronized" in that clicking on one image loaded a large image that wasn't representative of the thumbnail. That' where the sort function comes in. Seems to work fine except the very last item in the last row is always a "blank", the source it outputs is: <a href=><img src=thumbnail_dir/large_pics>.

I'm thinking php wants to just stick something there since the table specifies 3 items per row and in the absence of an image it just throws that blank link in.

Back to the drawing board... :roll:

Posted: Mon Nov 29, 2004 7:03 pm
by raging radish
Now it works as intended, with the addition of an "if" conditional before sending to the browser:

Code: Select all

$val=array_shift($fotos);
	       $val_lg=array_shift($fotos_lg);
			if (is_dir($val)) {
			     break;
			     }
		print "<td><a href=".$val_lg."><IMG src=".$val."></a></td>";
Maybe not an elegant solution, but hey, it works.