View file name

Ask about general coding issues or problems here.

Moderators: egami, macek, gesf

Post Reply
Xerpher
New php-forum User
New php-forum User
Posts: 164
Joined: Tue Aug 27, 2002 8:25 pm
Location: Ontario, Canada
Contact:

Mon Mar 24, 2003 7:53 pm

$_SERVER['PHP_SELF'];
Terra Design, my business and sole / huge PHP project... a fully dynamic and adaptive site engine.

mcandrey
New php-forum User
New php-forum User
Posts: 10
Joined: Tue Mar 25, 2003 3:59 am
Location: bucharest-romania
Contact:

Tue Mar 25, 2003 4:17 am

... or you can use the folowing:

basename($HTTP_SERVER_VARS['REQUEST_URI'])

Xerpher
New php-forum User
New php-forum User
Posts: 164
Joined: Tue Aug 27, 2002 8:25 pm
Location: Ontario, Canada
Contact:

Tue Mar 25, 2003 10:14 am

mcandrey wrote:... or you can use the folowing:

basename($HTTP_SERVER_VARS['REQUEST_URI'])

Sorry to say, but thats an awful way, it will return nothing if for example the url is "http://www.terradesign.ws/".

smm wrote:The problem with $_SERVER['PHP_SELF']; is that it returns the whole location. Do you know how to just return the file name? Even better, the file name without postfix like .php? Im working on a photo gallery, and the filename should help me refer to both file, picture, thumbnail, and comment function..

Why didn't you say so to begin with :P
You could use this method which will get the filename and remove the last 4 characters (removing '.php')

Code: Select all

substr(basename($_SERVER['PHP_SELF']),0,strlen(basename($_SERVER['PHP_SELF'])) - 4)

However if your going to have weird filename extensions which could be more or less than 4 ('.???') then:

Code: Select all

$fileshards = array_reverse(split("\.", basename($_SERVER['PHP_SELF'])), false);
print strtolower($fileshard[0]); // should output your filename as long as there is only 1 period

If you think your going to end up having some files with more than one just reply here and I'll add something that can get you past that problem. =)

Good luck.
Terra Design, my business and sole / huge PHP project... a fully dynamic and adaptive site engine.

Post Reply
  • Information
  • Who is online

    Users browsing this forum: No registered users and 5 guests