Soy un corredor Posted March 8, 2006 Share Posted March 8, 2006 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 https://forums.phpfreaks.com/topic/4482-display-only-file-name/ Share on other sites More sharing options...
annihilate Posted March 8, 2006 Share Posted March 8, 2006 Have a look at the basename function[a href=\"http://uk.php.net/basename\" target=\"_blank\"]http://uk.php.net/basename[/a] Link to comment https://forums.phpfreaks.com/topic/4482-display-only-file-name/#findComment-15615 Share on other sites More sharing options...
keeB Posted March 9, 2006 Share Posted March 9, 2006 [a href=\"http://us2.php.net/manual/en/function.strstr.php\" target=\"_blank\"]http://us2.php.net/manual/en/function.strstr.php[/a]:) Link to comment https://forums.phpfreaks.com/topic/4482-display-only-file-name/#findComment-15629 Share on other sites More sharing options...
kenrbnsn Posted March 9, 2006 Share Posted March 9, 2006 Besides the basename() function, you should look at the [a href=\"http://www.php.net/pathinfo\" target=\"_blank\"]pathinfo[/a]() function.Ken Link to comment https://forums.phpfreaks.com/topic/4482-display-only-file-name/#findComment-15633 Share on other sites More sharing options...
Soy un corredor Posted March 9, 2006 Author Share Posted March 9, 2006 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] Link to comment https://forums.phpfreaks.com/topic/4482-display-only-file-name/#findComment-15634 Share on other sites More sharing options...
Soy un corredor Posted March 9, 2006 Author Share Posted March 9, 2006 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? Link to comment https://forums.phpfreaks.com/topic/4482-display-only-file-name/#findComment-15636 Share on other sites More sharing options...
kenrbnsn Posted March 9, 2006 Share Posted March 9, 2006 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=3The 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]<?phpecho $_GET['id'];?>[/code]Ken Link to comment https://forums.phpfreaks.com/topic/4482-display-only-file-name/#findComment-15679 Share on other sites More sharing options...
txmedic03 Posted March 9, 2006 Share Posted March 9, 2006 [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 https://forums.phpfreaks.com/topic/4482-display-only-file-name/#findComment-15698 Share on other sites More sharing options...
Zane Posted March 9, 2006 Share Posted March 9, 2006 if you use the key SCRIPT_FILENAME on $_SERVERyou'll get just the filename[code]$fname = $_SERVER['SCRIPT_FILENAME'];[/code] Link to comment https://forums.phpfreaks.com/topic/4482-display-only-file-name/#findComment-15706 Share on other sites More sharing options...
txmedic03 Posted March 9, 2006 Share Posted March 9, 2006 $_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 https://forums.phpfreaks.com/topic/4482-display-only-file-name/#findComment-15737 Share on other sites More sharing options...
Soy un corredor Posted March 9, 2006 Author Share Posted March 9, 2006 [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 https://forums.phpfreaks.com/topic/4482-display-only-file-name/#findComment-15788 Share on other sites More sharing options...
Soy un corredor Posted March 10, 2006 Author Share Posted March 10, 2006 Any ideas? Link to comment https://forums.phpfreaks.com/topic/4482-display-only-file-name/#findComment-16016 Share on other sites More sharing options...
txmedic03 Posted March 10, 2006 Share Posted March 10, 2006 [code]<?phpif ( 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 https://forums.phpfreaks.com/topic/4482-display-only-file-name/#findComment-16054 Share on other sites More sharing options...
Recommended Posts
Archived
This topic is now archived and is closed to further replies.