Specialty Forums
Security and Virus
General Hardware
CPUs/Overclocking
Networking
Digital Photo/Video
Office Software
PC Gaming
Console Gaming
Programming
Database
Web Development
Digital Home

General Forums
Windows XP
Windows Vista
Windows 95/98
Windows Me
Windows NT
Windows 2000
Win Server 2008
Win Server 2003
Windows 3.1
Linux
PDAs
BeOS
Novell Netware
OpenVMS
Solaris
Disk Op. System
Unix
Mac
OS/2

Drivers
Driver Scan
Driver Forum

Software
Automatic Updates

BIOS Updates

My Computing.Net

Solution Center

Free IT eBook

Howtos

Site Search

Message Find

RSS Feeds

Install Guides

Data Recovery

About

Home
Reply to Message Icon Go to Main Page Icon

PHP current folder

Original Message
Name: rade
Date: October 24, 2006 at 13:05:13 Pacific
Subject: PHP current folder
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


Report Offensive Message For Removal


Response Number 1
Name: Michael J (by mjdamato)
Date: October 24, 2006 at 13:15:35 Pacific
Subject: PHP current folder
Reply: (edit)
$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


Report Offensive Follow Up For Removal

Response Number 2
Name: rade
Date: October 24, 2006 at 14:10:50 Pacific
Subject: PHP current folder
Reply: (edit)
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


Report Offensive Follow Up For Removal

Response Number 3
Name: Michael J (by mjdamato)
Date: October 24, 2006 at 14:53:37 Pacific
Subject: PHP current folder
Reply: (edit)
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


Report Offensive Follow Up For Removal

Response Number 4
Name: rade
Date: October 25, 2006 at 05:27:06 Pacific
Subject: PHP current folder
Reply: (edit)
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


Report Offensive Follow Up For Removal

Response Number 5
Name: Michael J (by mjdamato)
Date: October 25, 2006 at 07:05:53 Pacific
Subject: PHP current folder
Reply: (edit)
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


Report Offensive Follow Up For Removal


Response Number 6
Name: rade
Date: October 25, 2006 at 08:07:33 Pacific
Subject: PHP current folder
Reply: (edit)
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


Report Offensive Follow Up For Removal

Response Number 7
Name: rade
Date: October 25, 2006 at 08:27:06 Pacific
Subject: PHP current folder
Reply: (edit)
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


Report Offensive Follow Up For Removal

Response Number 8
Name: Michael J (by mjdamato)
Date: October 25, 2006 at 11:10:42 Pacific
Subject: PHP current folder
Reply: (edit)
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


Report Offensive Follow Up For Removal



Use following form to reply to current message:

   Name: From My Computing.Net Settings
 E-Mail: From My Computing.Net Settings

Subject: PHP current folder

Comments:

 
  Homepage URL (*): 
Homepage Title (*): 
         Image URL: 
 


Data Recovery Software




acer 312T BIOS problem

K7 Turbo possible max fsb?

Pc anywher problem

WinFLP & OE/Outlook2003

Computer resets after a few minutes


The information on Computing.Net is the opinions of its users. Such opinions may not be accurate and they are to be used at your own risk. Computing.Net cannot verify the validity of the statements made on this site. Computing.Net and Computing.Net, LLC hereby disclaim all responsibility and liability for the content of Computing.Net and its accuracy.
PLEASE READ THE FULL DISCLAIMER AND LEGAL TERMS BY CLICKING HERE

All content ©1996-2007 Computing.Net, LLC