duancifu6769 2013-11-18 20:18
浏览 259
已采纳

在Windows和Linux上获取绝对URL路径

I am including a file called includes/db.php which in turn requires connection_details.php

The issue I am having is I must read the content of db.php with this following line.

$dbSettingsFile = realpath( dirname( __FILE__ ) )."\connection-details.txt";
$lines = file($dbSettingsFile);//file in to an array

But if I am in turn already including the db.php file from a file that is something like. /subfolder/file.php

with the line

require('../includes/db.php');

But this will always work on windows but am I right in saying because of the backward slash in the $dbSettingsFile variable it will not work in linux? I have to be very specific like this because if you removed the realpath and ran file.php it would not pick up the correct path.

  • 写回答

1条回答 默认 最新

  • doutang7415 2013-11-18 20:28
    关注

    You should always use absolute filename, because you can't be sure about base directory where search for relative path will start.

    Use

    require( __DIR__ . '/../includes/db.php' );

    to be sure.

    Slash character '/' will work as directory separator in both Windows and Linux. Be careful - in Windows filenames 'db.php' and 'DB.php' are the same file but not in Linux.

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

报告相同问题?

悬赏问题

  • ¥15 k8s部署jupyterlab,jupyterlab保存不了文件
  • ¥15 ubuntu虚拟机打包apk错误
  • ¥199 rust编程架构设计的方案 有偿
  • ¥15 回答4f系统的像差计算
  • ¥15 java如何提取出pdf里的文字?
  • ¥100 求三轴之间相互配合画圆以及直线的算法
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题