PathUtility

Class with helper functions for file paths.

Table of Contents

Methods

basename()  : string
Returns trailing name component of path
basenameDuringBootstrap()  : string
Returns filename part of a path Early during bootstrap there is no TYPO3_CONF_VARS yet so the setting for the system locale is also unavailable. The filename part is determined with a regular expression to avoid issues with locales.
dirname()  : string
Returns parent directory's path
dirnameDuringBootstrap()  : string
Returns parent directory's path Early during bootstrap there is no TYPO3_CONF_VARS yet so the setting for the system locale is also unavailable. The path of the parent directory is determined with a regular expression to avoid issues with locales.
getAbsolutePathOfRelativeReferencedFileOrPath()  : string
Gets the (absolute) path of an include file based on the (absolute) path of a base file
getAbsoluteWebPath()  : string
Creates an absolute URL out of really any input path, removes '../' parts for the targetPath
getCanonicalPath()  : string
Resolves all dots, slashes and removes spaces after or before a path.
getCommonPrefix()  : string|null
Gets the common path prefix out of many paths.
getPublicResourceWebPath()  : string
Dedicated method to resolve the path of public extension resources
getRelativePath()  : string|null
Gets the relative path from a source directory to a target directory.
getRelativePathTo()  : string|null
Gets the relative path from the current used script to a given directory.
hasProtocolAndScheme()  : bool
Tries to guess whether a given URL hast protocol and (optional) scheme.
isAbsolutePath()  : bool
Checks if the $path is absolute or relative (detecting either '/' or 'x:/' as first part of string) and returns TRUE if so.
isAllowedAdditionalPath()  : bool
Evaluates a given path against the optional settings in `$GLOBALS['TYPO3_CONF_VARS']['BE']['lockRootPath']`.
isExtensionPath()  : bool
Checks whether the given path is an extension resource
pathinfo()  : string|array<string|int, string>
Returns parent directory's path
sanitizeTrailingSeparator()  : string
Normalizes a trailing separator.
stripPathSitePrefix()  : string
Strip first part of a path, equal to the length of public web path including trailing slash

Methods

basename()

Returns trailing name component of path

public static basename(string $path) : string

Since basename() is locale dependent we need to access the filesystem with the same locale of the system, not the rendering context.

Parameters
$path : string
Tags
see
http://www.php.net/manual/en/function.basename.php
Return values
string

basenameDuringBootstrap()

Returns filename part of a path Early during bootstrap there is no TYPO3_CONF_VARS yet so the setting for the system locale is also unavailable. The filename part is determined with a regular expression to avoid issues with locales.

public static basenameDuringBootstrap(string $path) : string
Parameters
$path : string
Return values
string

dirname()

Returns parent directory's path

public static dirname(string $path) : string

Since dirname() is locale dependent we need to access the filesystem with the same locale of the system, not the rendering context.

Parameters
$path : string
Tags
see
http://www.php.net/manual/en/function.dirname.php
Return values
string

dirnameDuringBootstrap()

Returns parent directory's path Early during bootstrap there is no TYPO3_CONF_VARS yet so the setting for the system locale is also unavailable. The path of the parent directory is determined with a regular expression to avoid issues with locales.

public static dirnameDuringBootstrap(string $path) : string
Parameters
$path : string
Return values
string

Path without trailing slash

getAbsolutePathOfRelativeReferencedFileOrPath()

Gets the (absolute) path of an include file based on the (absolute) path of a base file

public static getAbsolutePathOfRelativeReferencedFileOrPath(string $baseFilenameOrPath, string $includeFileName) : string

Does NOT do any sanity checks. This is a task for the calling function, e.g. call GeneralUtility::getFileAbsFileName() on the result.

Parameters
$baseFilenameOrPath : string

The name of the file or a path that serves as a base; a path will need to have a '/' at the end

$includeFileName : string

The name of the file that is included in the file

Tags
see
GeneralUtility::getFileAbsFileName()

Resolves all dots and slashes between that paths of both files. Whether the result is absolute or not, depends on the base file name.

If the include file goes higher than a relative base file, then the result will contain dots as a relative part.

  base:    abc/one.txt
  include: ../../two.txt
  result:  ../two.txt

The exact behavior, refer to getCanonicalPath().

Return values
string

The (absolute) path of the include file

getAbsoluteWebPath()

Creates an absolute URL out of really any input path, removes '../' parts for the targetPath

public static getAbsoluteWebPath(string $targetPath[, bool $prefixWithSitePath = true ]) : string

TODO: And this exactly is a big issue as it mixes file system paths with (relative) URLs TODO: Additionally it depends on the current request and can not do its job on CLI TODO: deprecate entirely and replace with stricter API

Until we have a replacement for this API, the safest way to call this method is by providing absolute filesystem paths and use \TYPO3\CMS\Core\Utility\PathUtility::getPublicResourceWebPath whenever possible.

Parameters
$targetPath : string

can be "../typo3conf/ext/myext/myfile.js" or "/myfile.js"

$prefixWithSitePath : bool = true

Don't use this argument. It is only used by TYPO3 in one place, which are subject to removal.

Return values
string

something like "/mysite/typo3conf/ext/myext/myfile.js"

getCanonicalPath()

Resolves all dots, slashes and removes spaces after or before a path.

public static getCanonicalPath(string $path) : string

..

Parameters
$path : string

Input string

Return values
string

Canonical path, always without trailing slash

getCommonPrefix()

Gets the common path prefix out of many paths.

public static getCommonPrefix(array<string|int, string> $paths) : string|null
  • /var/www/domain.com/typo3/sysext/frontend/
  • /var/www/domain.com/typo3/sysext/em/
  • /var/www/domain.com/typo3/sysext/file/ = /var/www/domain.com/typo3/sysext/
Parameters
$paths : array<string|int, string>

Paths to be processed

Return values
string|null

getPublicResourceWebPath()

Dedicated method to resolve the path of public extension resources

public static getPublicResourceWebPath(string $resourcePath[, bool $prefixWithSitePath = true ]) : string
Parameters
$resourcePath : string
$prefixWithSitePath : bool = true

Don't use this argument. It is only used by TYPO3 in one place, which is subject to removal.

Internal

This method should not be used for now except for TYPO3 core. It may be removed or be changed any time

Return values
string

getRelativePath()

Gets the relative path from a source directory to a target directory.

public static getRelativePath(string $sourcePath, string $targetPath) : string|null

The allowed TYPO3 path is checked as well, thus it's not possible to go to upper levels.

Parameters
$sourcePath : string

Absolute source path

$targetPath : string

Absolute target path

Return values
string|null

getRelativePathTo()

Gets the relative path from the current used script to a given directory.

public static getRelativePathTo(string $absolutePath) : string|null

The allowed TYPO3 path is checked as well, thus it's not possible to go to upper levels.

Parameters
$absolutePath : string
Return values
string|null

hasProtocolAndScheme()

Tries to guess whether a given URL hast protocol and (optional) scheme.

public static hasProtocolAndScheme(string $path) : bool

Scheme relative URLs match as well. Current implementation is two simple string operations.

This is just a guess. For a more detailed validation and parsing, use \TYPO3\CMS\Core\Utility\GeneralUtility::isValidUrl()

Parameters
$path : string
Internal
Return values
bool

isAbsolutePath()

Checks if the $path is absolute or relative (detecting either '/' or 'x:/' as first part of string) and returns TRUE if so.

public static isAbsolutePath(string $path) : bool
Parameters
$path : string
Return values
bool

isAllowedAdditionalPath()

Evaluates a given path against the optional settings in `$GLOBALS['TYPO3_CONF_VARS']['BE']['lockRootPath']`.

public static isAllowedAdditionalPath(string $path) : bool

Albeit the name BE/lockRootPath is misleading, this setting was and is used in general and is not limited to the backend-scope. The setting actually allows defining additional paths, besides the project root path.

Parameters
$path : string

Absolute path to a file or directory

Return values
bool

isExtensionPath()

Checks whether the given path is an extension resource

public static isExtensionPath(string $path) : bool
Parameters
$path : string
Return values
bool

pathinfo()

Returns parent directory's path

public static pathinfo(string $path[, int $options = PATHINFO_ALL ]) : string|array<string|int, string>

Since pathinfo() is locale dependent we need to access the filesystem with the same locale of the system, not the rendering context.

The valid flags for $options are the same as for the built-in phpinfo() function.

Parameters
$path : string
$options : int = PATHINFO_ALL
Tags
see
http://www.php.net/manual/en/function.pathinfo.php
Return values
string|array<string|int, string>

sanitizeTrailingSeparator()

Normalizes a trailing separator.

public static sanitizeTrailingSeparator(string $path[, string $separator = '/' ]) : string

(e.g. 'some/path' -> 'some/path/')

Parameters
$path : string

The path to be sanitized

$separator : string = '/'

The separator to be used

Return values
string

stripPathSitePrefix()

Strip first part of a path, equal to the length of public web path including trailing slash

public static stripPathSitePrefix(string $path) : string
Parameters
$path : string
Internal
Return values
string

        
On this page

Search results