Jump to content

Display ONLY file name


Soy un corredor

Recommended Posts

Hello.
I'm having a little trouble here. For the sake of simplicity, I'll give an example. Say I have a php file and the location of the file is:

[i]http://www.site.com/foldera/folderb/file.php[/i]

Each time I try to display [b]just[/b] the file name, I get this: [i]/foldera/folderb/file.php[/i]


How can I get just: [i]/file.php[/i]



Thanks!
Link to comment
Share on other sites

What are you trying to accomplish?

[!--quoteo--][div class=\'quotetop\']QUOTE[/div][div class=\'quotemain\'][!--quotec--]Sorry to post back so soon, but I have a follow-up question regarding my script [above]. Let's say, for example, that the file was view.php?id=3

The above code will only display view.php . How do I resolve this?[/quote]
The filename part is still "view.php" the "?id=3" is not part of the file. That is a parameter on the URL and will reside in the $_GET superglobal array:
[code]<?php
echo $_GET['id'];
?>[/code]

Ken
Link to comment
Share on other sites

[code]
<?php

$filename = substr($_SERVER['PHP_SELF'], 1);

if ( is_array($_GET) ) {
  $filename .= '?';
  while (list($key, $val)=each($_GET)) {
    $params .= '&'.$key.'='.$val;
  }
  $filename .= substr($params, 1);
}

echo $filename;

?>
[/code]

This will return the filename plus all of the url parameters, per your request.
Link to comment
Share on other sites

$_SERVER['SCRIPT_FILENAME'] does not return only the filename it also includes the relative path of the file. They also requested the parameters be passed along with the filename. I have posted two codes on here. One which echos the filename with parameters and one that will redirect to the sub-domain, though I believe this to be a poor choice, I have submitted the script. I can't keep you from doing silly things, so I might as well help you do them right.
Link to comment
Share on other sites

[code]$fname = $_SERVER['SCRIPT_FILENAME'];[/code]

This includes the file name AND the directory where it's located. Perhaps it'll be best if I explain my overall goal here...

My web host allows subdomains, but there are only "aliases" of directories. Meaning, [i]subdomain.site.com = site.com/subdomain[/i]. If a user goes to the address [i]site.com/subdomain/file.php[/i] I want them to be redirected to [i]subdomain.site.com/file.php[/i]. I hope that clears things up. Here is the code that I've been working with:

[code]<?php

$domain = 'site.com';
$wwwdomain = 'www.site.com';
$url = ''.$_SERVER['HTTP_HOST'];
$root = ''.$_SERVER['PHP_SELF'];
$file = basename($root);


if ( $domain == $url ) {

header("Location: http://subdomain.site.com/$file");
exit;

} elseif ( $wwwdomain == $url )

{

header("Location: http://subdomain.site.com/$file");
exit;

}

?>[/code]

THIS CODE WORKS GREAT BUT not when the address is [i]site.com/subdomain/file.php?[b]id=5[/b][/i] (for example). They will just be redirected to [i]site.com/subdomain/file.php[/i]. Any ideas?

Thanks!
Link to comment
Share on other sites

[code]
<?php

if ( count(explode('.',$_SERVER['HTTP_HOST'])) > 1 ) {
  $host = substr($_SERVER['HTTP_HOST'],strpos($_SERVER['HTTP_HOST'],'.')+1);
  $sub = substr($_SERVER['HTTP_HOST'],0,strpos($_SERVER['HTTP_HOST'],'.'));
} else {
  $host = $_SERVER['HTTP_HOST'];
  $sub = "www";
}

$dir = substr($_SERVER['SCRIPT_FILENAME'], strlen($_SERVER['DOCUMENT_ROOT'])+1, strrpos($_SERVER['SCRIPT_FILENAME'], '/')-strlen($_SERVER['DOCUMENT_ROOT'])-1);

$file = substr($_SERVER['SCRIPT_FILENAME'],strrpos($_SERVER['SCRIPT_FILENAME'],'/')+1);

$addr = 'http://'.$dir.'.'.$host.'/'.$file.'?'.implode('&',$_SERVER['argv']);

if ( $sub != $dir ) header("Location: ".$addr);

?>[/code]
Link to comment
Share on other sites

This thread is more than a year old. Please don't revive it unless you have something important to add.

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.

Guest
Reply to this topic...

×   Pasted as rich text.   Restore formatting

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.