简体   繁体   中英

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 insert 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'));

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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