Re: Photo gallery
- From: "J.O. Aho" <user@xxxxxxxxxxx>
- Date: Sun, 26 Jul 2009 16:23:04 +0200
Karl-Arne Gjersøyen wrote:
Now I have get forward some steps.. Still I haven't find the solution to create back-next links, but now I can at least show photos when a link are created. And it shows up in the same page :)
if you store all the file names into an array, then you can easily check which files are before and after. Instead of allowing the user from outside give you the image name, they give you the array cell number.
$images = array()
$catalogue = $_SERVER['DOCUMENT_ROOT'] . "/bulgariaferie/files/";
$cat_href = opendir($catalogue);
while($next = readdir($cat_href)) {
if(strpos($next,'jpg') !== false)
$images[] = $next;
}
if(isset($_GET['page']) && isset($_GET['photo']) && $_GET['page'] == "flat" && is_numeric($_GET['photo']) && isset($images[$_GET['photo']])) {
$previous_image = '';
$next_image = '';
if($_GET['photo'] > 0) {
$index = $_GET['photo'] -1;
$previous_image = '<a href="index.php?page=flat&photo='.
$index.'">'.$images[$index].'</a><br />';
}
if($_GET['photo'] < count($images)) {
$index = $_GET['photo'] +1;
$next_image = '<a href="index.php?page=flat&photo='.
$index.'">'.$images[$index].'</a><br />';
}
$str = <<<EOS
{$previous_image}
<div align="right">
<img src="files/{$images[$_GET['photo']]}" alt="Test img" />
</div>
{$next_image}
EOS;
}
Keep in mind this only is a example, not a perfect code and there are other ways to do this too.
Here is the source code:
<?php
// Scan the catalogue and list all photo files
$catalogue = $_SERVER['DOCUMENT_ROOT'] . "/bulgariaferie/files/";
$cat_href = opendir($catalogue);
while(($next = readdir($cat_href))){
if(strstr($next, "jpg")){
echo "<a href=\"index.php?page=flat&photo=$next\">$next</a><br />";
} }
closedir($cat_href);
// View images when the links are clicked
if((isset($_GET['page'])== "flat")&&(isset($_GET['photo'])!= "$next")){
You know that the $next is a string? You don't need the quotes around it, you should always avoid to put quotes around a variable.
The following test will give you two possible results, true or false
isset($_GET['photo'])
The same applies to
isset($_GET['page'])
you compare if true or false is not equal to the name of the last file in the directory. It really don't make any sense to make that comparison at all.
At the same time comparing if true/false is equal to "flat", "flat" is equal to true. I think this check will be a lot better for you
if(isset($_GET['page']) && isset($_GET['photo']) && $_GET['page'] == "flat" && $_GET['photo'] != $next) {
Now you check first if the get variables are set or not, then you compare the values of the cells in the GET array.
$next = $_GET['photo'];
You should make more checking on the value of $_GET['photo'], as it's here, I can access all files that the web server daemon has the right to read, at least run a:
$next = str_replce(array('..','/'),'',$_GET['photo']);
this way the user can't move out of the directory to find files.
echo "<div align=\"right\">\n";
echo "<img src=\"files/$next\" alt=\"Test img\" />\n";
echo "</div>\n";
It can be far easier to see what you type if you use
$str = <<<EOS
<div align="right">
<img src="files/{$next}" alt="Test img" />
</div>
EOS;
I still recommend that you don't do echo in your scripts until at the last stage, store "output" to a variable, return that variable from the function and decide if you want to print it out from the outside.
--
//Aho
.
- Follow-Ups:
- Re: Photo gallery
- From: Karl-Arne Gjersøyen
- Re: Photo gallery
- From: Karl-Arne Gjersøyen
- Re: Photo gallery
- References:
- Photo gallery
- From: Karl-Arne Gjersøyen
- Re: Photo gallery
- From: Jerry Stuckle
- Re: Photo gallery
- From: Karl-Arne Gjersøyen
- Re: Photo gallery
- From: Jerry Stuckle
- Re: Photo gallery
- From: Karl-Arne Gjersøyen
- Re: Photo gallery
- From: Jerry Stuckle
- Re: Photo gallery
- From: Karl-Arne Gjersøyen
- Re: Photo gallery
- From: Karl-Arne Gjersøyen
- Photo gallery
- Prev by Date: Re: Photo gallery
- Next by Date: Re: Predefined Variable
- Previous by thread: Re: Photo gallery
- Next by thread: Re: Photo gallery
- Index(es):
Relevant Pages
|