Board index   FAQ   Search  
Register  Login
Board index php forum :: php coding PHP coding => General

View file name

Ask about general coding issues or problems here.

Moderators: macek, egami, gesf

View file name

Postby smm » Mon Mar 24, 2003 2:42 pm

Hello! Does someone knows what php code I can use to return one files own name. My problem is: I have a 200 files named 1.php, 2.php...200.php, and i want each file to automatically view the file name e.g. 1.php when i load 1.php in internet explorer
smm
New php-forum User
New php-forum User
 
Posts: 3
Joined: Mon Mar 24, 2003 2:31 pm

Postby Xerpher » Mon Mar 24, 2003 7:53 pm

$_SERVER['PHP_SELF'];
Xerpher
New php-forum User
New php-forum User
 
Posts: 164
Joined: Tue Aug 27, 2002 8:25 pm
Location: Ontario, Canada

Postby mcandrey » Tue Mar 25, 2003 4:17 am

... or you can use the folowing:

basename($HTTP_SERVER_VARS['REQUEST_URI'])
mcandrey
New php-forum User
New php-forum User
 
Posts: 10
Joined: Tue Mar 25, 2003 3:59 am
Location: bucharest-romania

re:

Postby smm » Tue Mar 25, 2003 9:14 am

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..
smm
New php-forum User
New php-forum User
 
Posts: 3
Joined: Mon Mar 24, 2003 2:31 pm

Postby Xerpher » 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.
Xerpher
New php-forum User
New php-forum User
 
Posts: 164
Joined: Tue Aug 27, 2002 8:25 pm
Location: Ontario, Canada

re

Postby smm » Tue Mar 25, 2003 11:13 am

that worked well! Thanks for good help
smm
New php-forum User
New php-forum User
 
Posts: 3
Joined: Mon Mar 24, 2003 2:31 pm


Return to PHP coding => General

Who is online

Users browsing this forum: No registered users and 5 guests

Sponsored by Sitebuilder Web hosting and Traduzioni Italiano Rumeno and antispam for cPanel.