dsa1234569 2014-09-01 19:58
浏览 28

php - 如何获取服务器的当前目录但不是当前文件名?

SOLUTION

$path = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
$feedtitle = str_replace(" ", "", $feedtitle);
$feedtitle = strtolower($feedtitle);
$path = substr($path, 0, strrpos($path, "/"));
$feedlink = "http://" . "$path" . "/"  . "$feedtitle" . '.xml';

PROBLEM

I am trying to generate a name for an rss feed from another field (feed title). In my query I have :

$path = $_SERVER['SERVER_NAME'] . $_SERVER['REQUEST_URI'];
$feedtitle = str_replace(" ", "", $feedtitle);
$feedtitle = strtolower($feedtitle);
$feedlink = "$path" . "$feedtitle" . '.xml';

If feedtitle is my rss feed, $feedlink will have the value myrssfeed.xml so that part of the code works but I'm having problems with the path. Instead of writing the current path the above writes the current path & the name of the current file! e.g. if the script I'm using for this is named feedlink.php the path that is stored in the $feedlink variable is :

mywebsite.com/mydir/feedlink.phpmyrssfeed.xml

How do I remove the name of the script?

  • 写回答

2条回答 默认 最新

  • du2986 2014-09-01 20:05
    关注

    Assuming that you requested the site as mywebsite.com/mydir/feedlink.php you should consider removing everything past the last / and then add your filename to that:

    $path = substr($path, 0, strrpos($path, "/") + 1);
    
    评论

报告相同问题?

悬赏问题

  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP