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 武汉岩海低应变分析软件,导数据库里不显示波形图
  • ¥15 CreateBitmapFromWicBitmap内存释放问题。
  • ¥30 win c++ socket
  • ¥15 CanMv K210开发板实现功能
  • ¥15 C# datagridview 栏位进度
  • ¥15 vue3页面el-table页面数据过多
  • ¥100 vue3中融入gRPC-web
  • ¥15 kali环境运行volatility分析android内存文件,缺profile
  • ¥15 写uniapp时遇到的问题
  • ¥15 vs 2008 安装遇到问题