Forum Moderators: coopster
$dir = "example.com/galleries/person/images/";
$dir1 = dir($dir);
$count = count(glob($dir.'*'))/2; // # of files in directory, not including thumbnails
$pics_per_page = 16;
$num_pages = $count / $pics_per_page; // # of full pages
settype($num_pages,"integer"); // make $num_pages a whole number
// determine if an extra page is needed
if ($count % $pics_per_page!=0) {
$num_pages = $num_pages + 1;
}
$page = isset($_GET['page'])? $_GET['page'] : 1; // if page is set, get page, else page = 1
$start_value = ($page * $pics_per_page) - $pics_per_page + 1;//display pics
print "\n<table>\n";
$j = $start_value;
while ($j < ($start_value + $pics_per_page)) {
if (($file = $dir1->read())!= false) {
if (preg_match("/_.jpg$/", $file)) {
print "<tr><td><img src=\"".$dir.$j."_.jpg\">$j</td></tr>\n";
}
}
$j++;
}
closedir($dir1);
Also, while I'm here, I know I should probably do some security measures because of the $_GET, but if I'm not using any $_GETs (in any general script, not this particular one), there are still plenty of security exploits I have to worry about, right?
Jennifer
I'm not sure what exactly is causing your issue yet, but you may want to escape that period in your regular expression as it means more than just punctuation.
if (preg_match("/_\.jpg$/", $file)) {
Can I change glob to something else so that it doesn't even pick up the folders?
Jennifer
Well, I was thinking that it could be the . and .. folders, but wouldn't the preg_match that I have exclude them?
Yes, it will, but the $count variable is based on the number of elements returned by the glob() function earlier on, so your count will be 2 more than you expect and also why you are missing two of your jpgs when you loop.
I'm not a big fan of glob [webmasterworld.com]. You may be better off using dir-> to read the files into an array first, getting only those files that you want and count/process that resulting array instead.
I've now changed it to output the directory listing into an array.
$dir = "galleries/person/test/";
$d = dir($dir);
$files= array();
while (false!== ($entry = $d->read())) {
if ( substr($entry, 0, 1)=='.' ) continue;
$files[]= $entry;
}
$d->close();$count = count($files);
$pics_per_page = 16;$num_pages = $count / $pics_per_page; // # of full pages
settype($num_pages,"integer"); // make $num_pages a whole number
// determine if an extra page is needed
if ($count % $pics_per_page!=0) {
$num_pages = $num_pages + 1;
}
$page = isset($_GET['page'])? $_GET['page'] : 1; // if page is set, get page, else page = 1
$start_value = ($page * $pics_per_page) - $pics_per_page + 1;sort($files, SORT_NUMERIC);
$j = $start_value;
print "<ul>";
while ( $j < ($start_value + $pics_per_page)) {
print "<li>" . $files[$j] . " - " . $j . "</li>\n";
$j++;
}
print "</ul>";
My problem is that I don't want the array index to start off at 0, I want it to start off at 1. How can I do that?
Jennifer
I'm having a problem with image_001.jpg being skipped again but my junk image is trying to be displayed, but only on certain galleries. There is no difference in the numbering scheme of the images in different directorys and I have no statements that should only affect certain galleries.
To make this a little easier to understand, this is how the gallery is structured:
Person 1 ($gallery)
- section 1 ($type)
- - page 1 ($page)
- - page 2 ($page)
- section 2 ($type}
- - page 1 ($page)
- - page 2 ($page)
Person 2 ($gallery)
- section 1 ($type)
- - page 1 ($page)
- - page 2 ($page)
- section 2 ($type}
- - page 1 ($page)
- - page 2 ($page)
Videos ($gallery)
- video 1 ($type)
- - page 1 ($page)
- - page 2 ($page)
- video 2 ($type)
- - page 1 ($page)
and so on. All of the types in the Person 1, 2, and 3 galleries might start off with image_001.jpg, but then all of the types in Person 4 might start off with image_002.jpg and end with the junk image. Within the videos gallery, some types work, some don't.
_____________________________________________
$dir = $gallery."/images/".$type."/";
$d = dir($dir);
$abs_dir = "/galleries/".$dir;
//output all files in $dir into an array, then exclude thumbnails and . directory
$files= array();
$files[] = 'junk'; //waste [0] element in array
$index = 1;
while (false!== ($entry = $d->read())) {
if (preg_match("/^_/", $entry) ¦¦ substr($entry, 0, 1)=='.') {
continue;
}
$files[]= $entry;
}
$d->close();
$count = count($files) - 1; //-1 to delete [0] element
$pics_per_page = 16;
sort($files, SORT_REGULAR);
unset($files[0]); //waste [0] element in array
//determine the number of pages needed and what image each page should start with
$num_pages = $count / $pics_per_page; // # of full pages
settype($num_pages,"integer"); // make $num_pages a whole number
// determine if an extra page is needed
if ($count % $pics_per_page!=0) {
$num_pages = $num_pages + 1;
}
$page = isset($_GET['page'])? $_GET['page'] : 1; // if page is set, get page, else page = 1
$start_value = ($page * $pics_per_page) - $pics_per_page + 1;
//begin displaying pics
$j = $start_value;
while ($j < ($start_value + $pics_per_page)) {
if ($j > ($count)) {
break;
} else {
if ($j%4 == 1) {
$template->assign_block_vars('row', array());
}
$template->assign_block_vars('row.cells', array(
'ABS_DIR' => $abs_dir,
'FILE_NAME' => $files[$j])
);
$j++;
}
}
files sorting in directory [webmasterworld.com]
List files in directory - issue [webmasterworld.com]
Sorting fgetcsv information [webmasterworld.com]