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.

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

报告相同问题?

悬赏问题

  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?