How can I get just the filename of the originating document?

How can I get just the filename of the originating document?
$ref=@$_SERVER[HTTP_REFERER]; gives me the full http path
$ref=@$_SERVER[REQUEST_URI]; gives me the /directoryname/filename
I only want the filename. Nothing else.
Can you help?
Thanks
Sas
LVL 1
sasnaktivAsked:
Who is Participating?
 
Dave BaldwinConnect With a Mentor Fixer of ProblemsCommented:
No need to do that.  This should work.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>Get Filename</title>
</head>
<body>
<?php 
$fn = $_SERVER['SCRIPT_NAME'];
$fn2 = explode('/',$fn);
foreach($fn2 as $value) {
	$fn3 = $value;
	}
echo $fn3;
 ?>
</body>
</html>

Open in new window

0
 
Dave BaldwinFixer of ProblemsCommented:
On many servers, 'SCRIPT_NAME' gives you just the filename.  However, this is not guaranteed.  Each server and admin may provide a different set of values because they all come from the server.

http://php.net/manual/en/reserved.variables.server.php

I use this program to see what my servers provide.
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN"
 "http://www.w3.org/TR/html4/loose.dtd">

<html>
<head>
<title>PHP Server Variables</title>
</head>
<body>
<h1>PHP Server Variables</h1>
<?php

//reset($_SERVER);
foreach($_SERVER as $key => $value) {
    echo "<b>$key :</b> $value<br />\n";
}
?>
</body>
</html>

Open in new window

0
 
sasnaktivAuthor Commented:
Thanks for your quick response Dave,
Your list gives me a lot of information that I'm not looking for.
And it doesn't give me the simple, isolated filename that I am looking for.
I just want the 'filename' not its directory, not its path.
Can you isolate the 'filename'?
Sas
0
Cloud Class® Course: CompTIA Cloud+

The CompTIA Cloud+ Basic training course will teach you about cloud concepts and models, data storage, networking, and network infrastructure.

 
Dave BaldwinFixer of ProblemsCommented:
Apparently not.
0
 
sasnaktivAuthor Commented:
So much for PHP.
Do you know how I can delete everything before the 'filename'?
That might be a solution.
Thanks for the help, Dave
0
 
sasnaktivConnect With a Mentor Author Commented:
That's nice Dave, except for one thing.
It shouldn't be $fn = $_SERVER['SCRIPT_NAME'];

Instead it should be: $fn = $_SERVER['HTTP_REFERER'];

Because $fn = $_SERVER['SCRIPT_NAME']; returns the filename of the current document.
Whereas $fn = $_SERVER['HTTP_REFERER']; returns the filename of the referring document. And it's the referring document that I've been looking to capture.

Thanks for your patience and knowledge.
Sas
0
 
sasnaktivAuthor Commented:
Much appreciated, Dave.
Sas
1
 
Dave BaldwinFixer of ProblemsCommented:
You're welcome.  You said 'originating document' and I thought you meant the name of the page.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.