Computing.Net > Forums > Web Development > PHP current folder

Computer Problems? Computing.Net has over 1,000,000 posts about all things technology related! Over 90% answered within 24 hours! Click here to start participating now! Also, be sure to check out the New User Guide.

PHP current folder

Reply to Message Icon

Name: rade
Date: October 24, 2006 at 13:05:13 Pacific
OS: WinXP Home Edition
CPU/Ram: 1gb
Comment:

Hey,

I'm doing a webpage and have probelms with the menu (which I'm making with PHP).

To check if I want the submenu to be visible I check the current location and then use a if-statement:

// define our current location
$current_url = $_SERVER['PHP_SELF'];

if(($current_url == "/second/index.php") or ($current_url == "/second/file.php") or ($current_url == "/second/form.php") or ($current_url == "/second/this.php")){

...Then show the submenu.

Now I'm looking for a way to only check what the current folder is, so that I don't have to check all the files separetly. Something like this:

if($current_folder == "/second/"){
...Then show the submenu }

Can you guys help me?

"Real men don't use backups, they post their stuff on a public ftp server and let the rest of the world make copies." - Linus Torvalds



Sponsored Link
Ads by Google

Response Number 1
Name: Michael J (by mjdamato)
Date: October 24, 2006 at 13:15:35 Pacific
Reply:

$current_folder = basename(dirname(__FILE__));

This will set the value to the folder in which the currently running file resides (w/o the slashes).

Using your example above the code would be written:

if($current_folder == "second"){
...Then show the submenu }

Michael J


0

Response Number 2
Name: rade
Date: October 24, 2006 at 14:10:50 Pacific
Reply:

Thanks for the help but, it doesn't work.

Am I supposed to leave this as it is:

$current_folder = basename(dirname(__FILE__));

Or modify anything in it?

Peace, rade

"Real men don't use backups, they post their stuff on a public ftp server and let the rest of the world make copies." - Linus Torvalds


0

Response Number 3
Name: Michael J (by mjdamato)
Date: October 24, 2006 at 14:53:37 Pacific
Reply:

When you say "It doesn't work" please be specific. Are you getting errors? If so, what are they? Have you done ANY error checking?

If I have a php file with the code below, I can put it in any folder and when I run that page it will display the name of the folder that the file is in.

<?php
$current_folder = basename(dirname(__FILE__));
echo $current_folder;
?>

Michael J


0

Response Number 4
Name: rade
Date: October 25, 2006 at 05:27:06 Pacific
Reply:

Oh, sorry. I was in a hurry last time I wrote.

To be specific: I have the whole navigation code in leftmenu.php, which I then have included with php include(); in all the sites.

Now I came to think of, does the server read the PHP in leftmenu.php before or after it has included it in eg main.php? Because if it reads it and converts it to html code before it's included in main.php, that's why it didn't work.

I'm at the office now but will test more when I come home. Catch you later!

"Real men don't use backups, they post their stuff on a public ftp server and let the rest of the world make copies." - Linus Torvalds


0

Response Number 5
Name: Michael J (by mjdamato)
Date: October 25, 2006 at 07:05:53 Pacific
Reply:

Sorry, I don't follow you. When you "include" a file it is processed in-line. In other words it's the same as if you had copied and pasted the code from the include file at the same position as you put the include command.

Try echoing the value of $current_folder to the page right after you create it and see if it is what you are expecting. You should also echo the value just before you use it in your conditional statement. You might have a problem with variable scope.

Forgive me if I am pointing out the obvious. But, when developing you need to do these simple debugging processes to ensure your values are what you are expecting whenever your conditionals (IF statements) are not functioning properly.

Michael J


0

Related Posts

See More



Response Number 6
Name: rade
Date: October 25, 2006 at 08:07:33 Pacific
Reply:

It's okay, I'm very glad you are willing to help. Now:

This is how the files are organised:
------
root
/main
- index.php
/second
- index.php
- file.php
- form.php
/system
- leftmenu.php
------

leftmenu.php contains the whole navigation code.
all the other files are sites which have this code there where the navigation is supposed to be:

<?php
include ("../system/leftmenu.php");
?>

This is my goal: When the user is browsing any file in the folder named "second" a submenu would appear. This is what I need the if-statement for.

So if you are browsing main/index.php the navigation would show links to:
- main/index.php
- second/index.php

And if you are browsing any file in "second", eg second/file.php the navigation would show links to:
- main/index.php
- second/index.php
-- second/file.php
-- second/form.php

I tested having this in the leftmenu.php and was browsing main/index.php:

$current_folder = basename(dirname(__FILE__));
print($current_folder);

And it printed "system". Because the leftmenu.php is in the "sytem" folder.
BUT, when I had this code in the leftmenu.php and browsed main/index.php:

$current_url = $_SERVER['PHP_SELF'];
print($current_url);

It printed "/main/index.php".

And having this code in main/index.php itself:

$current_folder = basename(dirname(__FILE__));
print($current_folder);

Printed "main".

So what would solve my problem (I think), is by somehow getting the current folder by using this method:

$current_url = $_SERVER['PHP_SELF'];

What do you think?
/Rade


"Real men don't use backups, they post their stuff on a public ftp server and let the rest of the world make copies." - Linus Torvalds


0

Response Number 7
Name: rade
Date: October 25, 2006 at 08:27:06 Pacific
Reply:

I did it!

This is how:

$current_url = $_SERVER['PHP_SELF'];
$pieces = explode("/", $current_url);
$current_folder = $pieces[1];

So first I took the $current_url, which would print "second/index.php" (when browsing second/index.php).

Then I split $current_url up with the explode() function into the array $pieces, so every part would be separated by a "/". This gives us the parts:
$pieces[1] = "second"
$pieces[2] = "index.php"

Then I put the $current_folder to the same as $pieces[1] (which is equal to the current folder).

And finally, I used it in my if-statement:

if($current_folder == "second"){
...Then show the submenu

Thank you for your help! And IF you come up with another solution, I'd love to hear that too.

Peaces & Good luck, Rade


"Real men don't use backups, they post their stuff on a public ftp server and let the rest of the world make copies." - Linus Torvalds


0

Response Number 8
Name: Michael J (by mjdamato)
Date: October 25, 2006 at 11:10:42 Pacific
Reply:

OK, you are putting your code into the leftmenu.php file and want it to return the root folder of the file that included leftmenu.php. Here's an easier option:

$current_folder = basename(dirname($_SERVER['PHP_SELF']));

Michael J


0

Sponsored Link
Ads by Google
Reply to Message Icon






Post Locked

This post is quite old and has been locked from receiving new replies. Please create a new posting instead.


Go to Web Development Forum Home


Sponsored links

Ads by Google


Results for: PHP current folder

index.php?id=main/news Doesnt Work www.computing.net/answers/webdevel/indexphpidmainnews-doesnt-work/2370.html

Help setting up Apache and PHP www.computing.net/answers/webdevel/help-setting-up-apache-and-php/3750.html

Flash help www.computing.net/answers/webdevel/flash-help/2067.html