View Full Version : get current file name as variable
paulj000
07-19-03, 02:54 AM
Hi,
What is the easiest way to get the current file name in the users URL bar as a variable without the domain and sub directories?
Like if they are at http://www.mydomain.com/dir1/dir2/2003_a.php
then
$currentFile = "2003_a.php"
thanks
ChristGuy
07-19-03, 04:08 AM
Greetingz...
$currentFile = $_SERVER["SCRIPT_NAME"];
Hope that helps...
paulj000
07-19-03, 06:52 AM
Hi ChristGuy
This is what I tried out:
<?
$currentFile = $_SERVER["SCRIPT_NAME"];
print $currentFile;
?>
On one of the servers I use this was the output:
/cgi-bin/php
On the other server it output:
/test/03.php
where "test" was one directory deep from site root.
I can delete the directory info if I need to on the second one but I have no idea how to handle the first output!
Any ideas?
ChristGuy
07-20-03, 04:09 AM
You could try using explode...
$currentFile = $_SERVER["SCRIPT_NAME"];
$parts = Explode('/', $currentFile);
print $parts[count($parts)];
That should work...
paulj000
07-23-03, 03:39 AM
Sorry, Let me try to be a little more clear. I am looking for a script that will output only "03.php".
The first url was "http://www.domain1.com/test/03.php"
Output was simply "/cgi-bin/php"
The second url was "http://ww.domain2.com/test/03.php"
Output was "/test/03.php"
How should the script be to output only "03.php" -- on both servers?
Thanks ChristGuy
ChristGuy
07-23-03, 07:58 AM
Greetinz...
If you use the second script what are the results??
And if you use it in a different file on server 1 what do you get?
Hi !
Hope this help
$url ="http://yahoo.com/abcdef/2153abc.gif";
preg_match("/[^\/]+$/",$url,$matches);
$file_name = $matches[0];
print "$file_name";
it will print 2153abc.gif
Regard
Kevin
paulj000
07-23-03, 05:19 PM
Hi ChirstGuy,
The output on both servers was nothing. The page was blank when I used the following code only:
<?
$currentFile = $_SERVER["SCRIPT_NAME"];
$parts = Explode('/', $currentFile);
print $parts[count($parts)];
?>
----------------
Hi Kevin,
I need to be able to have the script get the current file name without supplying the url. The script has to determine what the URL is on it's own and then break it down and output the current page/file name.
Thanks guys
amailer
07-23-03, 05:57 PM
<?
$currentFile = $_SERVER["SCRIPT_NAME"];
$img = array_pop(explode("/", $currentFile));
echo $img;
?>
see if that works :/
Man Down
07-23-03, 07:28 PM
Christ Guys code will work if you change it to:
<?
$currentFile = $_SERVER["SCRIPT_NAME"];
$parts = Explode('/', $currentFile);
print $parts[count($parts) - 1];
?>
ChristGuy
07-24-03, 10:48 AM
Thanx... Man Down....
That would explain the null result... *slaps side of head* Doh...
paulj000
07-24-03, 11:40 AM
Hi there,
This updated code...
<?
$currentFile = $_SERVER["SCRIPT_NAME"];
$parts = Explode('/', $currentFile);
print $parts[count($parts) - 1];
?>
On server 1 (Hostway) ouptut: php
On server 2 (OnSmart) output: file.php
Yay!! it works on the second server and almost on the first!
Hostway is a good company but maybe they are doing something weird with their config. Earlier in this thread the output was:
cgi-bin/php
Any thoughts on how to get it to work on the Hostway servers?
Thanks a bunch, this code is going to help me out a whole bunch!
Man Down
07-24-03, 12:26 PM
ON hostway server try this code:
<?
$currentFile = $_SERVER["PHP_SELF"];
$parts = Explode('/', $currentFile);
print $parts[count($parts) - 1];
?>
paulj000
07-24-03, 03:07 PM
Case Closed.
This one using "PHP_SELF" works on both servers. Even better.
Thanks a bunch fellas for all of your help!
- Paul
What about $_SERVER['PHP_SELF'] ? :confused:
The filename of the currently executing script, relative to the document root.
Man Down
07-24-03, 06:28 PM
If the file that executes $_SERVER[PHP_SELF] is in a sub directory it would make it folder/filename.ext
Oh gees, i didn't even see this whole 2nd page :D
vBulletin® v3.6.4, Copyright ©2000-2008, Jelsoft Enterprises Ltd.