donglaoe2979 2015-10-10 16:53
浏览 29
已采纳

获取项目目录PHP的路径

I need get full path to my web-project directory, as I am using single entry point so I doing this:

$_SERVER['SCRIPT_FILENAME']

then remove index.php and then I get something like this: /var/www/myproject, and then I can use it for require some stuff files like configuration etc. But when I call this method (e.g. for include some stylesheets) in my php files for rendering I getting 404 error for this stylesheet and what I getting in browser console:

http://localhost/var/www/myproject/bower_components/bootstrap/dist/css/bootstrap.min.css Failed to load resource: the server responded with a status of 404 (Not Found)

Obviosly that path should looks like:

http://localhost/myproject/bower_components/bootstrap/dist/css/bootstrap.min.css

So the question is how I can properly get path to my project for proper include files in PHP and load assets in HTML ? And the approach what I am using now it ok? Or I need to do somehow better? Thanks!

  • 写回答

1条回答 默认 最新

  • douqiangchuai7674 2015-10-10 16:55
    关注

    The problem is there are TWO type of adresses:

    1) LOCAL, which are accessible throw your scripts from INSIDE your server. You use them if need to include file inside your script and such, but not from outside by browser.

    2) WEB addresses, which are accessible by browser from OUTSIDE. You use them if need to give some file to browser, like style-sheet or JavaScript script.

    Try $_SERVER['SERVER_NAME'] . $_SERVER["DOCUMENT_ROOT"] for this task.

    Detailed description for server environment variables

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 HLs设计手写数字识别程序编译通不过
  • ¥15 Stata外部命令安装问题求帮助!
  • ¥15 从键盘随机输入A-H中的一串字符串,用七段数码管方法进行绘制。提交代码及运行截图。
  • ¥15 TYPCE母转母,插入认方向
  • ¥15 如何用python向钉钉机器人发送可以放大的图片?
  • ¥15 matlab(相关搜索:紧聚焦)
  • ¥15 基于51单片机的厨房煤气泄露检测报警系统设计
  • ¥15 Arduino无法同时连接多个hx711模块,如何解决?
  • ¥50 需求一个up主付费课程
  • ¥20 模型在y分布之外的数据上预测能力不好如何解决