/    Sign up×
Community /Pin to ProfileBookmark

Directory List Navigation

I’m developing an image gallery where there are heaps of images in various countries, and from within the countries, different regions.
My filing system for the images on the server are as follows:

httpdocs
-images
–countries
—country name
—-region

I want to start off at the countries folder, and display all of the subfolders contained within.
That’s the easy part.
My code attached:

<?PHP
$heading = “Countries”;
print “<big><b>”;
print $heading;
print “</b></big><br>”;
$dir = “images/countries”;
$handle = opendir($dir);
$num = 0;
while (false !== ($file = readdir($handle))) {
if ($file != “.” && $file != “..”) {
echo “<a href=”$dir/$file” class=”directory”>$file</a><br>”;
$num++;
}
}
closedir($handle);
?>

Each sub-folder of “dir-countries” is listed in <a href> format.

The problem is that the link is not pointing at this moment to a file, but it’s pointing to more folders.
I want to be able to click on a country, and have the list refresh by now reading the subfolders of the country I just clicked.

Any ideas?

Pyro helped out the other day showing me how to use an href to activate a function in a php script, but I can’t seem to see how I could use a similar technique in the above code.

Bit of a long one, but hope someone has done this kind of thing before.
Thanks,
John

to post a comment
PHP

21 Comments(s)

Copy linkTweet thisAlerts:
@pyroDec 04.2003 — Yep, I've done it before... let me go find my code. ?

It's about 100 lines long, so I'll just attach it.

[upl-file uuid=deca4419-a757-4fa5-a572-2c0e5a9b7d03 size=3kB]browser.zip[/upl-file]
Copy linkTweet thisAlerts:
@jasnjohnauthorDec 04.2003 — Superb!

I need to read it through and customise it for my site, but the controls are exactly what I'm looking to accomplish the effect.

Excellent script,

many thanks,


I'm so glad it had already been done.?
Copy linkTweet thisAlerts:
@pyroDec 04.2003 — Glad it was what you were looking for. ?
Copy linkTweet thisAlerts:
@jasnjohnauthorDec 04.2003 — Hi Pyro,

not sure if you remember the "Calling a function in php from HTML" script you helped out with the other day?

Well, this is to go together with the gallery.

I'll be using a file count in the directories that sets a counter limit.

I'll display the first image on the screen, then, by clicking a hyperlink, I'll activate a php function that increments the count, and changes the file name of the image to display accordingly.

The beauty of it all is that I currently have about 40 different html documents for all the countries I have galleries for, but with this single page, I can do it all.

Now that's streamlining.

Thanks again!
Copy linkTweet thisAlerts:
@pyroDec 04.2003 — It's stuff like that that makes you fall in love with PHP... ?

I've used this script (and another similar) to be able to browser my entire server. And, as a simpler clone, I've used a script that will just list all the files in a single directory.
Copy linkTweet thisAlerts:
@jasnjohnauthorDec 05.2003 — Hi Pyro,

I'm having a little trouble with that script you attached:

See attached snippet;

if (!isset($_GET["folder"])) { #if a folder has not been passed via the query string

$folder = $_
SERVER["DOCUMENT_ROOT"]; #setting the start directory

}

else { #if a folder has been passed via the query string

$folder = urldecode($_GET["folder"]); #set to the current directory (directory that was passed)


$rootlen = strlen($_SERVER["DOCUMENT_ROOT"]); #get the length of the DOCUMENT_ROOT

$folderlen = strlen($folder); #get the length of the directory trying to be opened

if ($folderlen >= $rootlen) { #if a hacker is trying to pass a query string less than the DOCUMENT_ROOT, and thus compromise secrurity

$handle = opendir($folder); #opens the directory so we can read the files out

# Making an array containing the files in the current directory:


while ($file = readdir($handle)) { #loop through the files

$array[] = $file; #make an array of the files

}

closedir($handle); #close the directory

sort($array); #sort the files in alphabetical order
}

else {

echo "<span style="color:red;">You do not have access to this folder.</span>"; #message to print if folder was higher than the DOCUMENT_ROOT


well, from another page that I'm linking to, with this script on, I'm passing a string via an href (eg; <a href="travelindex.php?folder=images/countries">

The script above is waiting for a query string to be passed (which I am doing), but it keeps reverting the "else" portion of the above code, thus, printing to the screen "You do not have access to this folder".

The php file that contains the code is in my root folder, and the "folder" query string I'm passing is for a folder contained in root.

I've tried just passing ?folder=images, but that too failed.

Any ideas?

Thanks,

John
Copy linkTweet thisAlerts:
@pyroDec 05.2003 — Yes. This script requires that you pass the full path, from the document root. For what I needed this for, I never had to be able to, or wanted to, pass the value in any other way than simply using the PHP script. Anyway, the value passed must be from the document root, and must be [url=http://us3.php.net/manual/en/function.urlencode.php]urlencoded()[/url].
Copy linkTweet thisAlerts:
@jasnjohnauthorDec 05.2003 — I was hoping I'd be able to find out how to go about this once you'd let me know how it should be done, but alas no;

The code that's sending the Query String is in html. Should I add in php for the urlencode()?

I tried various things with little (no) luck.

Like;

<a href="thepage.php?directory=urlencode(usr/local..../oursite.com/images/countries), but that had no desired effect.

I also tried;

<a href="thepage.php?<?php $dir = $_SERVER["DOCUMENT_ROOT"]."/images/countries"; urlencode($dir);?>

but no desired effect either.

Any further prompts would be appreciated.

Thanks,

John
Copy linkTweet thisAlerts:
@pyroDec 05.2003 — Try this one:

[code=php]<a href="thepage.php?folder=<?php echo urlencode($_SERVER["DOCUMENT_ROOT"]."/images/countries"); ?>">countries</a>[/code]
Copy linkTweet thisAlerts:
@jasnjohnauthorDec 05.2003 — Well, I'm happy I was working along the right lines, however, once again, your answer has proven correct.

Are you ever wrong?

Many thanks for your help...again

John:rolleyes:
Copy linkTweet thisAlerts:
@pyroDec 05.2003 — Lol... yep, definitly... ?

Anyway, I'm glad to help when I can. ?
Copy linkTweet thisAlerts:
@jasnjohnauthorDec 07.2003 — I'm trying to display an image <img src=...

but I've only got the full server path of the image to work with (ie, usr/local/host/blah blah blah/folder/1.jpg)

and it can't be used between the <img tags.

Is there anyway to convert the path into something that can be used in between <img> tags?

Again, this is using the script you attached in a previous response above.

I've tried the following, but no joy:

$imgname = $folder."/".$filename;

print "<img src="".$imgname."">";

I've been customising the code so that it does specifically what I want, and your code has taught me a lot, it's just little things like this that cause me to get stuck.

Well, only 1 week since I picked up my PHP book, so it can't be bad.?
Copy linkTweet thisAlerts:
@pyroDec 07.2003 — Try this:

[code=php]<?PHP
$rootpath = "usr/local/host/blah blah blah/folder/1.jpg";
$absolutepath = str_replace($_SERVER['DOCUMENT_ROOT'], "", $rootpath);
echo "<img src="$absolutepath" alt="">";
?>[/code]
Copy linkTweet thisAlerts:
@jasnjohnauthorDec 07.2003 — Shiver me timbers pyro, it didn't work.

I've got the following done.

As stated several times before, I'm developing a gallery, so I will need a variable so that I can click my way through the pics.

Again, using the script you attached recently;

So, my rootpath is;

$num = "1"; (This is my counter variable)

$rootpath = $folder."/$num.jpg";

$absolutepath = str_replace($_SERVER['DOCUMENT_ROOT'], "", $rootpath);


further on in a <div>, I've got;

print "<div class="travelimg">";

if ($l_file) {

print "<img_src="$absolutepath"_alt="">";

}

print "</div>";

I used the if statement to only display an image when it is there.

Anyway, the div came up blank.

Any other ideas?
Copy linkTweet thisAlerts:
@jasnjohnauthorDec 07.2003 — I can't understand why the img isn't being displayed.

For diagnosis, I just tried printing both $rootpath, and $absolutepath.

$absolutepath gives exactly the path I would have given in html for the <img src, so, maybe there's something up with my <div>.

Will check it out and report back.
Copy linkTweet thisAlerts:
@pyroDec 07.2003 — The understrikes in the following should be removed:

print "<img_src="$absolutepath"_alt="">";
Copy linkTweet thisAlerts:
@jasnjohnauthorDec 08.2003 — Thanks pyro.

Well, can you believe I've another doozy.

I think by the time I have it all working, I'll be able to take credit for about 5% of having it work, the rest goes to you.

Well, here it is;

Again, using the code we've been discussing;

I have a list of countries (folders) displayed down the left of the screen. I click on a folder, and it takes me into a sub-folder, which then displays the cities. Once again, I click on a city and it will then display the first image into a <div>.

I have a variable $num which is used in the path to display the image (eg; $rootpath = $folder."/$num.jpg";

I then have 2x functions. One for incrementing $num

function numinc() {

global $num;

$num++;

}

and the second for decrementing it.

I have the following code which is waiting to receive Query String;

if (isset ($_GET['action']) && $_GET['action'] == "increment") {

numinc();

}

and likewise for numdec()

I am now trying to use 2 hyperlinks to activate the functions;

print "<a href="".$_SERVER['PHP_SELF']."?action=increment&num=".$num."">increment</a><br>";

and likewise for numdec()

The problem is when I click the link, the page returns to the top level of the folder lists (eg; countries).


I've a feeling that the $_SERVER['PHP_SELF'] is missing the information about where the page is actually at (eg; which folder is being viewed.)

Can you see what's happening and what could be the cause?

If you need the url, let me know and I'll put it up.

Thanks,

John
Copy linkTweet thisAlerts:
@pyroDec 08.2003 — The current directory should be held in the $folder variable, so you should be able to pass it along by adding this to your query string:

&folder=".urlencode($folder)."
Copy linkTweet thisAlerts:
@jasnjohnauthorDec 09.2003 — OK, we're nearly there. The page remains active without reseting the path, however, the count variable isn't incrementing properly. It's a simple thing, but I think because the variable is being used in various places for various things, the $num variable isn't being changed by the numinc() and numdec() functions, probably because where $num is inserted into various parts of the code isn't not recognising $num as global.

I'm a bit stumped on how to actually get around this.

I've attached some code from where the $num variable begins, so maybe you'll see throughout where the $num is used;

For diagnosis, I printed out the $num variable so I could see what happened when numinc() and numdec() were activated. Well, the count started at 1, and after numinc(), to 2, and it would go no further. numdec() would make the count jump 0. I'm just figuring that $num isn't being changed throughout the necessary parts of the code correctly.

Are there any obvious mistakes?

Sorry to keep on, and thanks for the help so far


// Set the count variable, determine the rootpath, add the default image to the end and str_replace for use as <img src

$num = "1";

$rootpath = $folder."/$num.jpg";

$absolutepath = str_replace($_SERVER['DOCUMENT_ROOT'], "", $rootpath);


// Get the page header started

echo $header;

include "header.php";

include "menubar.html";

print "<div class="directorylist">";

// Clear out the unwanted _vti_cnf server folders

if ($l_file && $l_fold = "_
vti_cnf") {

$l_fold = "";

}

echo $tablestart.$parent.$l_fold.$tableend;

print "</div>";


// Here's the code that will call the functions if they receive the correct result from the query string

if (isset ($_GET['action']) && $_GET['action'] == "increment") {

numinc();

}

if (isset ($_GET['action']) && $_GET['action'] == "decrement") {

numdec();

}



// Here are the functions that will change the image

function numinc() {

global $num;

$num++;

}

function numdec() {

global $num;

$num--;

}






print "<div class="travelimg">";

if ($l_file) {

print "<img src=";

print """;

print $absolutepath;

print ""><br>";

print "<a href="".$_SERVER['PHP_SELF']."?action=increment&num=".$num."&folder=".urlencode($folder)."">increment</a><br>";

print "<a href="".$_
SERVER['PHP_SELF']."?action=decrement&num=".$num."&folder=".urlencode($folder)."">decrement</a><br>";

print $num;

}

print "</div>";
Copy linkTweet thisAlerts:
@pyroDec 09.2003 — Try changing this:

[code=php]$num = "1";[/code]

to:

[code=php]$num = (isset($_GET['num'])) ? $_GET['num'] : "1";[/code]
Copy linkTweet thisAlerts:
@jasnjohnauthorDec 09.2003 — Thanks.

It's pretty much there now, although the incrementing $num that is part of the image path is updating 1 behind the displayed $num (ie, for diagnostics, I'm printing $num to screen, and I can see it update upon each click of numinc() or numdec(), but the $num in the img path is always 1 count behind.

I'll try and work this one out myself anyway, but just wanted to say many thanks again for the support.

Greatly appreciated.

John
×

Success!

Help @jasnjohn spread the word by sharing this article on Twitter...

Tweet This
Sign in
Forgot password?
Sign in with TwitchSign in with GithubCreate Account
about: ({
version: 0.1.9 BETA 5.19,
whats_new: community page,
up_next: more Davinci•003 tasks,
coming_soon: events calendar,
social: @webDeveloperHQ
});

legal: ({
terms: of use,
privacy: policy
});
changelog: (
version: 0.1.9,
notes: added community page

version: 0.1.8,
notes: added Davinci•003

version: 0.1.7,
notes: upvote answers to bounties

version: 0.1.6,
notes: article editor refresh
)...
recent_tips: (
tipper: @AriseFacilitySolutions09,
tipped: article
amount: 1000 SATS,

tipper: @Yussuf4331,
tipped: article
amount: 1000 SATS,

tipper: @darkwebsites540,
tipped: article
amount: 10 SATS,
)...