$_SERVER["SCRIPT_NAME"]

PHP programming forum. Ask questions or help people concerning PHP code. Don't understand a function? Need help implementing a class? Don't understand a class? Here is where to ask. Remember to do your homework!

Moderator: General Moderators

Post Reply
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

$_SERVER["SCRIPT_NAME"]

Post by JayBird »

how can i do the following

$_SERVER["SCRIPT_NAME"] contains something like /pl_generator/generated/test.php

Where test.php can be anything of any length.

How can i just return /pl_generator/generated/ - basically, just the root relative path.

Mark
User avatar
Ixplodestuff8
Forum Commoner
Posts: 60
Joined: Mon Feb 09, 2004 8:17 pm
Location: Queens, New York

Post by Ixplodestuff8 »

If you know the filename, try this

Code: Select all

<?php
str_replace ( $filename, '', $_SERVER['SCRIPT_NAME'] );
?>
if not, try somthing like this

Code: Select all

<?php
$script_name = $_SERVER['SCRIPT_NAME'];
$path = explode ( '/', $script_name );
$counter = count ( $path );
$path[$counter - 1] = NULL;
$output = implode ( '/', $path );

?>
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

See [php_man]pathinfo[/php_man].

Watch out for the dot bug (excerpt from user comments):
If you use the function on a dir and the last dir in the path has a . in its name, then the function thinks that it's a file
Last edited by McGruff on Tue Aug 09, 2005 4:46 pm, edited 1 time in total.
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

$path = dirname($_SERVER['SCRIPT_NAME']);

??
User avatar
redhair
Forum Contributor
Posts: 300
Joined: Fri May 30, 2003 4:36 pm
Location: 53.23N-6.57E
Contact:

Post by redhair »

or: dirname(dirname(__FILE__))

just see: http://www.php.net/manual/en/function.dirname.php
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

i dont want the full server path though, just the path shown in the URL without the filename.

And i don't know what thie filename will be.

Mark
McGruff
DevNet Master
Posts: 2893
Joined: Thu Jan 30, 2003 8:26 pm
Location: Glasgow, Scotland

Post by McGruff »

Rgr - define a config constant or something with the absolute path to the web root, then subtract.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

McGruff wrote:Rgr - define a config constant or something with the absolute path to the web root, then subtract.
How exactly would i do that?

Mark
User avatar
markl999
DevNet Resident
Posts: 1972
Joined: Thu Oct 16, 2003 5:49 pm
Location: Manchester (UK)

Post by markl999 »

$path = dirname($_SERVER['SCRIPT_NAME']);
Did you try that? That will give you the directory relative to the document root.
User avatar
JayBird
Admin
Posts: 4524
Joined: Wed Aug 13, 2003 7:02 am
Location: York, UK
Contact:

Post by JayBird »

aha!

I missed that one buddy, works a treat!

Thanks

Mark
Post Reply