1

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!

3 Answers 3

3

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'
Sign up to request clarification or add additional context in comments.

4 Comments

Sorry, I meant relative path, but your solution worked without the first slash. Just wanted to check to make sure it wasn't different in PHP. Thanks for your help!!
Why absolute paths is better in this specific case?
@sanmai If you include FolderA/file.php from somewhere else, the relativity of the path changes. Using an absolute path ensures correctness.
As I said, I'm not good with PHP, and the files I am using to send push notifications to my iOS apps require me to use a relative path, not an absolute path (I didn't write them). That would have saved me much trouble though. Thanks!
0

True.

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

Comments

0

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.