get file name for url php

不问归期 提交于 2021-02-04 16:08:04

问题


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

GOOGLE

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

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!