php relative path to file

Are PHP include paths relative to the file or the calling code?

I’m having trouble understanding the ruleset regarding PHP relative include paths. If I run file A.PHP- and file A.PHP includes file B.PHP which includes file C.PHP, should the relative path to C.PHP be in relation to the location of B.PHP, or to the location of A.PHP? That is, does it matter which file the include is called from, or only what the current working directory is- and what determines the current working directory?

6 Answers 6

It’s relative to the main script, in this case A.php. Remember that include() just inserts code into the currently running script.

That is, does it matter which file the include is called from

If you want to make it matter, and do an include relative to B.php, use the __FILE__ constant (or __DIR__ since PHP 5.2 IIRC) which will always point to the literal current file that the line of code is located in.

php relative path to file. Смотреть фото php relative path to file. Смотреть картинку php relative path to file. Картинка про php relative path to file. Фото php relative path to file

php relative path to file. Смотреть фото php relative path to file. Смотреть картинку php relative path to file. Картинка про php relative path to file. Фото php relative path to file

@Pekka got me there, but just want to share what I learned:

getcwd() returns the directory where the file you started executing resides.

dirname(__FILE__) returns the directory of the file containing the currently executing code.

Using these two functions, you can always build an include path relative to what you need.

e.g., if b.php and c.php share a directory, b.php can include c.php like:

no matter where b.php was called from.

In fact, this is the preferred way of establishing relative paths, as the extra code frees PHP from having to iterate through the include_path in the attempt to locate the target file.

Tested on PHP 5.4.3 (Build Date : May 8 2012 00:47:34).

The accepted answer of Pekka is incomplete and, in a general context, misleading. If the file is provided as a relative path, the called language construct include will search for it in the following way.

Files are included based on the file path given or, if none is given, the include_path specified. If the file isn’t found in the include_path, include will finally check in the calling script’s own directory and the current working directory before failing.

Источник

realpath

(PHP 4, PHP 5, PHP 7, PHP 8)

realpath — Returns canonicalized absolute pathname

Description

Parameters

The path being checked.

Whilst a path must be supplied, the value can be an empty string. In this case, the value is interpreted as the current directory.

Return Values

realpath() returns false on failure, e.g. if the file does not exist.

For case-insensitive filesystems realpath() may or may not normalize the character case.

The function realpath() will not work for a file which is inside a Phar as such path would be a virtual path, not a real one.

On Windows, junctions and symbolic links to directories are only expanded by one level.

Note: Because PHP’s integer type is signed and many platforms use 32bit integers, some filesystem functions may return unexpected results for files which are larger than 2GB.

Examples

Example #1 realpath() example

The above example will output:

Example #2 realpath() on Windows

On windows realpath() will change unix style paths to windows style.

echo realpath ( ‘/windows/system32’ ), PHP_EOL ;

echo realpath ( ‘C:\Program Files\\’ ), PHP_EOL ;
?>

The above example will output:

See Also

User Contributed Notes 15 notes

As you can so, it also produces Yoda-speak. 🙂

realpath() is just a system/library call to actual realpath() function supported by OS. It does not work on a path as a string, but also resolves symlinks. The resulting path might significantly differs from the input even when absolute path is given. No function in this notes resolves that.

namespace MockingMagician \ Organic \ Helper ;

Источник

include_once, relative path in php

I have 3 files: home, failed_attempt, login.

The file home and failed_attempt all refer to login file.

The annoying thing is that they throw a mistake saying that the login file doesnt exist. home will throw an exception if i do this, but failed_attempt wont.

5 Answers 5

Things like realpath() and __DIR__ are your friends when it comes to creating paths in PHP.

Since include and require are language constructs (rather than functions) they don’t need brackets. Generally speaking you’d use include to include «templates» (output files) and require to include PHP files such as classes.

So you could use something like:

(use dirname(__FILE__) instead of __DIR__ on PHP realpath() returns false if the file doesn’t exist and attempting to require false; spaffs out a PHP error.

Alternatively you can just use absolute paths like:

php relative path to file. Смотреть фото php relative path to file. Смотреть картинку php relative path to file. Картинка про php relative path to file. Фото php relative path to file

Here are three possible solutions. The second are really just work-arounds that use absolute paths in a clever way.

1: chdir into the correct directory

Note that this will only work if your StoredProcedure folder is in the topmost directory of any files that might need to include the files it contains.

2: Use absolute paths

Now before you say this is not portable, it actually depends on how you implement it. Here’s an example that works with Apache:

Источник

How do I format a PHP include() absolute (rather than relative) path?

On various pages throughout my PHP web site and in various nested directories I want to include a specific file at a path relative to the root.

What single command can I put on both of these pages.

. to include this page:

This does not work:

Does it matter that this is hosted in IIS on Windows?

9 Answers 9

From the perspective of PHP root is the top of the file system on the web server, not the root from the perspective of the web browser.

Most people do one of the below.

Define a constant, in a global configuration file, and use that in each call to require/include.

Or they use code like this.

Using the environmental variables may be dangerous in some cases and be the source of security issues.

If you give include() or require() (or the *_once versions) an absolute pathname, that file will be included. An absolute pathname starts with a «/» on unix, and with a drive letter and colon on Windows.

If you give a relative path (any other path), PHP will search the directories in the configuration value «include_path» in order, until a match is found or there are no more directories to search.

So, in short, to include an absolute filename, give an absolute filename. See also the function realpath().

If you want to set your own include «root», have a look at this question (specifically my answer of course 🙂

Источник

Absolute (or relative?) path in PHP

Sorry for asking it as it may be answered many times before, but my question is little bit different

(I’m accessing inc/include.php from index.php,

but in include.php, I need to get absolute path to APPLICATION root, not DOCUMENT_ROOT so in result, I need to be able to use this command

repeating, I do NOT WANT TO CALL IT LIKE

is there native function, if possible?

UPDATE:

I am wanting to get PATH by PHP as any open source need to have this path detection why? If I would want to include inc/include.php from ajax/1/somethiing.php, it success but inc/include.php then tries to include ajax/b.php instead of b.php

For Pekka:

Now look. From index.php, you’ll call inc/include.php

now, included file searchs for

It will work, but! If I would call include of inc/include.php from ajax/1/ajax.php, like this

it will work, but included file will try to include

6 Answers 6

is there native function, if possible?

no. The document root is the only thing you can get from PHP. (Note however that in your scenario, you can simply call include(«b.php»); because the script is still in the context of index.php.)

Re your update:

You can define a global application root in a central configuration file. Say you have config.php in your app root. Then do a

you still have to include the config file, and you have to use relative paths for it, e.g.

but once you have done that, you can work relative to the app root inside the script:

Источник

Добавить комментарий

Ваш адрес email не будет опубликован. Обязательные поля помечены *