问题
I wanted to know how to get the current file name in PHP to be able to load the same file in a different directory.
index.php
works.php
lang/de/
index.php
works.php
lang/pl/
index.php
works.php
<div id="language">
<a href="../../index.php" class="lang">EN</a>
<a class="lang" href="index.php">DE</a>
<a class="lang" href="../pl/index.php">PL</a>
</div>
The current method is redirecting the lang URL, allways to index.php. I would love to know how to redirect from works.php to lang/de/works.php without redirecting to lang/de/index.php
Sorry if i'm getting you confused
回答1:
I think this is what you want:
<?php
$page = basename($_SERVER['PHP_SELF']);
?>
<div id="language">
<a href="../../<?php echo $page;?>" class="lang">EN</a>
<a class="lang" href="<?php echo $page;?>">DE</a>
<a class="lang" href="../pl/<?php echo $page;?>">PL</a>
</div>
You would save the current file name in the $page variable. Then echo it out and replace index.php in your links.
回答2:
You should really google this stuff.
<?php basename($_SERVER['PHP_SELF']); ?>
Now that you added some code, here ya go.
<div id="language">
<a href="../../<?php echo basename($_SERVER['PHP_SELF']); ?>" class="lang">EN</a>
<a class="lang" href="<?php echo basename($_SERVER['PHP_SELF']); ?>">DE</a>
<a class="lang" href="../pl/<?php echo basename($_SERVER['PHP_SELF']); ?>">PL</a>
</div>
FYI, basename($_SERVER['PHP_SELF']);
get's the name of the current file and returns just that. In other words, it will return "index.php" or "works.php"
来源:https://stackoverflow.com/questions/18771157/get-file-name-for-url-php