How To Get File Name Only Without The Path Slashes In PHP
how to get file name only without the path slashes in php
how, to, get, file, name, only, without, the, path, slashes, in, php
How To Get File Name Only Without The Path Slashes In PHP
Post Description: how to get file name only without the path slashes in php
POST# 478
Posted On: Fri Feb 29, 2008 12:01 pm
web hosting
Topic: How To Get File Name Only Without The Path Slashes In PHP
i saw your post on how to get your path, but what i wanted to do was to only get the file name..

if i have a file called gallery.php and its in the /forums/gallery.php directory, i dont want the forums part.

well, i figure it.

if i use: $_SERVER['PHP_SELF']
print $_SERVER['PHP_SELF'];

OUTPUT:
/forums/gallery.php


if i have gallery.php?id=23: $_SERVER['REQUEST_URI']
print $_SERVER['REQUEST_URI'];

OUTPUT:
/forums/gallery.php?id=23
but i only want gallery.php

this is how i did it

i broke it down to an array:

<?
function GetFileName($php_self){
$filename = explode("/", $php_self); // THIS WILL BREAK DOWN THE PATH INTO AN ARRAY
$filename = array_reverse($filename ); // THIS WILL MAKE THE LAST ELEMENT THE FIRST
return $filename[0];
}
echo GetFileName($_SERVER['PHP_SELF']);
?>


Thu Mar 13, 2008 2:10 am
1
Joeri
Reply #1066
creative. just, would this not be easier:

echo basename($_server['request_uri'])

What do you think?

* name:  

* email:  

* Please enter comments:


Receive Replies on my Comments
(An email will be sent to you when someone replies to your comments)

Add image to comments
yes no             upload