开发者

Simple PHP Path Question

I'm new to PHP, so this is a very simple question. Let's say I have two folders: FolderA and FolderB. Within FolderA, I have a PHP file. Within FolderB, I have a certificate. If I wanted to inse开发者_StackOverflowrt the relative path to the certificate in the PHP file, would this be this path: ../FolderB/Certificate.pem?

Thanks for your help!


Your solution entirely depends on whether or not the file in FolderA is at the top of the "include tree".

A better solution would be to use an absolute path, eg

// 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';

To illustrate why an absolute path is better, consider this

// 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'


True.

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


Unless you involved yourself in set_include_path() then yes you would be correct!

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜