Jump to content


Photo

Display ONLY file name


  • Please log in to reply
12 replies to this topic

#1 Soy un corredor

Soy un corredor
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 08 March 2006 - 11:10 PM

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:

http://www.site.com/foldera/folderb/file.php

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


How can I get just: /file.php



Thanks!

#2 annihilate

annihilate
  • Members
  • PipPipPip
  • Advanced Member
  • 63 posts
  • LocationUK

Posted 08 March 2006 - 11:56 PM

Have a look at the basename function
[a href=\"http://uk.php.net/basename\" target=\"_blank\"]http://uk.php.net/basename[/a]
Personal site: NewEnigma  |  Java and JSF Discussion

#3 keeB

keeB
  • Staff Alumni
  • Advanced Member
  • 1,078 posts
  • LocationCalifornia

Posted 09 March 2006 - 12:22 AM

[a href=\"http://us2.php.net/manual/en/function.strstr.php\" target=\"_blank\"]http://us2.php.net/manual/en/function.strstr.php[/a]

:)

Come visit my site to see my latest projects
http://nick.stinemates.org/wordpress/


#4 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 09 March 2006 - 12:56 AM

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

Ken

#5 Soy un corredor

Soy un corredor
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 09 March 2006 - 01:04 AM

I added the basename() function to my current code and came up with this:

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



Works now ;)

¬°Gracias a todos!

#6 Soy un corredor

Soy un corredor
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 09 March 2006 - 01:16 AM

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?

#7 kenrbnsn

kenrbnsn
  • Staff Alumni
  • Advanced Member
  • 8,235 posts
  • LocationHillsborough, NJ, USA

Posted 09 March 2006 - 05:06 AM

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:
<?php
echo $_GET['id'];
?>

Ken

#8 txmedic03

txmedic03
  • Members
  • PipPipPip
  • Advanced Member
  • 313 posts
  • LocationCall, TX, USA

Posted 09 March 2006 - 07:03 AM

<?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;

?>

This will return the filename plus all of the url parameters, per your request.

SEMPER FIDELIS!

I can't stop you from doing something silly, but at least I can help you do it right.


#9 Zane

Zane
  • Administrators
  • Advanced Member
  • 4,134 posts

Posted 09 March 2006 - 08:00 AM

if you use the key SCRIPT_FILENAME on $_SERVER
you'll get just the filename


$fname = $_SERVER['SCRIPT_FILENAME'];

btn_donate_SM.gif Want to thank me? Contribute to my PayPal piggy-bank
 

172938.png

#10 txmedic03

txmedic03
  • Members
  • PipPipPip
  • Advanced Member
  • 313 posts
  • LocationCall, TX, USA

Posted 09 March 2006 - 12:09 PM

$_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.

SEMPER FIDELIS!

I can't stop you from doing something silly, but at least I can help you do it right.


#11 Soy un corredor

Soy un corredor
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 09 March 2006 - 03:12 PM

$fname = $_SERVER['SCRIPT_FILENAME'];

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, subdomain.site.com = site.com/subdomain. If a user goes to the address site.com/subdomain/file.php I want them to be redirected to subdomain.site.com/file.php. I hope that clears things up. Here is the code that I've been working with:

<?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;

}

?>

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

Thanks!

#12 Soy un corredor

Soy un corredor
  • New Members
  • Pip
  • Newbie
  • 5 posts

Posted 10 March 2006 - 03:01 AM

Any ideas?

#13 txmedic03

txmedic03
  • Members
  • PipPipPip
  • Advanced Member
  • 313 posts
  • LocationCall, TX, USA

Posted 10 March 2006 - 07:21 AM

<?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);

?>

SEMPER FIDELIS!

I can't stop you from doing something silly, but at least I can help you do it right.





0 user(s) are reading this topic

0 members, 0 guests, 0 anonymous users