Jump to content

Archived

This topic is now archived and is closed to further replies.

Soy un corredor

Display ONLY file name

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!

Share this post


Link to post
Share on other sites
Have a look at the basename function
[a href=\"http://uk.php.net/basename\" target=\"_blank\"]http://uk.php.net/basename[/a]

Share this post


Link to post
Share on other sites
[a href=\"http://us2.php.net/manual/en/function.strstr.php\" target=\"_blank\"]http://us2.php.net/manual/en/function.strstr.php[/a]

:)

Share this post


Link to post
Share on other sites
Besides the basename() function, you should look at the [a href=\"http://www.php.net/pathinfo\" target=\"_blank\"]pathinfo[/a]() function.

Ken

Share this post


Link to post
Share on other sites
I added the basename() function to my current code and came up with this:

[i]$php_self = ''.$_SERVER['PHP_SELF'];
$file = basename($php_self);[/i]


Works now ;)

[b]¬°Gracias a todos![/b]

Share this post


Link to post
Share on other sites
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 [i]view.php?id=3[/i]

The above code will only display [i]view.php[/i] . How do I resolve this?

Share this post


Link to post
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

Share this post


Link to post
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.

Share this post


Link to post
Share on other sites
if you use the key SCRIPT_FILENAME on $_SERVER
you'll get just the filename


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

Share this post


Link to post
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.

Share this post


Link to post
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!

Share this post


Link to post
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]

Share this post


Link to post
Share on other sites

×

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.