Reputation: 9293
URL of my site is:
http://mc.net46.net/ + folderName + fileName
For example:
http://mc.net46.net/mc/file01.php
http://mc.net46.net/mx/file05.php
folderName
is always two characters long.
$address = 'http://mc.net46.net'.$_SERVER["REQUEST_URI"];
result: http://mc.net46.net/mc/file01.php
- ok
$fname = substr($_SERVER["SCRIPT_NAME"],strrpos($_SERVER["SCRIPT_NAME"],"/")+1);
result: file01.php
- ok
Two questions:
Is this the correct way to get $address and $fname ?
How to get folderName?
Upvotes: 0
Views: 248
Reputation: 2559
Try this for another way to get your dynamic file names:
<?php
$fname = "http://mc.net46.net/mc/file01.php";
OR
$fname = $_SERVER["REQUEST_URI"];
$stack = explode('/', $fname);
$ss = end($stack);
echo $ss;
?>
Here for $fname
you can use this $fname = explode('/', $_SERVER["REQUEST_URI"]);
Upvotes: 5
Reputation: 13738
try
function getUriSegment($n) {
$segs = explode("/", parse_url($_SERVER['REQUEST_URI'], PHP_URL_PATH));
return count($segs)>0 && count($segs)>=($n-1)?$segs[$n] : '';
}
// if the url is http://www.example.com/foo/bar/wow
echo getUriSegment(1); //returns foo
echo getUriSegment(2); //returns bar
for more :- http://www.timwickstrom.com/server-side-code/php/php-get-uri-segments/
Upvotes: 1
Reputation: 1362
Getting the address looks correct to me. However, you can get the $fname and the folder name easily using explode, and array_pop
$stack = explode('/', $_SERVER["REQUEST_URI"]);
$fname = array_pop($stack);
$folderName = array_pop($stack);
EDIT:
Explaining how does this work: the explode
function will split the URI into ['', 'mc', 'file01.php']
for example. Now the function array_pop
takes out the last element ($fname = 'file01.php'
) from the array, that means after the first call the array will be ['', 'mc']
, and repeating the same action in the second call will will take out ($folderName = 'mc'
) as it will be the last element in the array and leave ['']
.
Upvotes: 2
Reputation: 6887
Use basename
$fname = basename("http://mc.net46.net/mc/file01.php")
RESULT = file01.php
Upvotes: 1