繁体   English   中英

简单的PHP路径问题

[英]Simple PHP Path Question

我是PHP的新手,所以这是一个非常简单的问题。 假设我有两个文件夹:FolderA和FolderB。 在FolderA中,我有一个PHP文件。 在FolderB中,我有一个证书。 如果我想在PHP文件中插入证书的相对路径, ../FolderB/Certificate.pem这是以下路径: ../FolderB/Certificate.pem

谢谢你的帮助!

您的解决方案完全取决于FolderA的文件是否位于“包含树”的顶部。

更好的解决方案是使用绝对路径,例如

// FolderA/file.php

// PHP 5.3 only
$path = realpath(__DIR__ . '/../FolderB/Certificate.pem');

// or if stuck with PHP 5.2 or earlier
$path = dirname(dirname(__FILE__)) . '/FolderB/Certificate.pem';

为了说明为什么绝对路径更好,请考虑以下因素

// FolderA/file.php
$path = '../FolderB/Certificate.pem';

// some-other-file-in-the-root-directory.php
include 'FolderA/file.php';
// $path is now incorrect as it points to
// ROOT . '../FolderB/Certificate.pem'

真正。

/* FolderA/test.php: */
var_dump(is_file('../FolderB/Certificate.pem'));

除非您自己参与set_include_path(),否则是正确的!

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM