Php путь до текущей папки

realpath

realpath() раскрывает все символические ссылки, переходы типа /./ , /../ и лишние символы / в пути path , возвращая канонизированный абсолютный путь к файлу.

Список параметров

Замечание:

Несмотря на то, что путь должен быть указан, переданное значение может быть пустой строкой. В этих случаях значение интерпретируется как текущая рабочая директория.

Возвращаемые значения

В случае успешного выполнения возвращает канонизированный абсолютный путь. Результирующий путь не содержит символических ссылок и компонентов наподобие /./ или ‘ /../ . Разделители \ и / в конце пути также удаляются.

realpath() возвращает false при неудаче, например, если файл не существует.

Замечание:

Запускаемый скрипт должен иметь права запуска на всех директориях в проверяемой иерархии, иначе realpath() вернёт false .

Замечание:

Для регистронезависимых файловых систем, realpath() может нормализовать или не нормализовать регистр символов.

Замечание:

Функция realpath() не будет работать с файлом внутри архива Phar, так как путь может быть не реальным, а виртуальным.

Замечание:

В Windows переходы и символические ссылки на каталоги расширяются только на один уровень.

Замечание: Так как тип integer в PHP является целым числом со знаком, и многие платформы используют 32-х битные целые числа, то некоторые функции файловых систем могут возвращать неожиданные результаты для файлов размером больше 2 Гб.

Примеры

Пример #1 Пример использования функции realpath()

chdir ( ‘/var/www/’ );
echo realpath ( ‘./../../etc/passwd’ ) . PHP_EOL ;

echo realpath ( ‘/tmp/’ ) . PHP_EOL ;
?>

Результат выполнения данного примера:

Пример #2 realpath() на Windows

На Windows realpath() изменит пути стиля Unix на стиль Windows.

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

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

Результат выполнения данного примера:

C:\WINDOWS\System32 C:\Program Files

Смотрите также

  • basename() — Возвращает последний компонент имени из указанного пути
  • dirname() — Возвращает имя родительского каталога из указанного пути
  • pathinfo() — Возвращает информацию о пути к файлу

User Contributed Notes 17 notes

Because realpath() does not work on files that do not
exist, I wrote a function that does.
It replaces (consecutive) occurences of / and \\ with
whatever is in DIRECTORY_SEPARATOR, and processes /. and /.. fine.
Paths returned by get_absolute_path() contain no
(back)slash at position 0 (beginning of the string) or
position -1 (ending)
function get_absolute_path ( $path ) $path = str_replace (array( ‘/’ , ‘\\’ ), DIRECTORY_SEPARATOR , $path );
$parts = array_filter ( explode ( DIRECTORY_SEPARATOR , $path ), ‘strlen’ );
$absolutes = array();
foreach ( $parts as $part ) if ( ‘.’ == $part ) continue;
if ( ‘..’ == $part ) array_pop ( $absolutes );
> else $absolutes [] = $part ;
>
>
return implode ( DIRECTORY_SEPARATOR , $absolutes );
>
?>

A test:
var_dump ( get_absolute_path ( ‘this/is/../a/./test/.///is’ ));
?>
Returns: string(14) «this/a/test/is»

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

namespace MockingMagician \ Organic \ Helper ;

class Path
/**
* There is a method that deal with Sven Arduwie proposal https://www.php.net/manual/en/function.realpath.php#84012
* And runeimp at gmail dot com proposal https://www.php.net/manual/en/function.realpath.php#112367
* @param string $path
* @return string
*/
public static function getAbsolute ( string $path ): string
// Cleaning path regarding OS
$path = mb_ereg_replace ( ‘\\\\|/’ , DIRECTORY_SEPARATOR , $path , ‘msr’ );
// Check if path start with a separator (UNIX)
$startWithSeparator = $path [ 0 ] === DIRECTORY_SEPARATOR ;
// Check if start with drive letter
preg_match ( ‘/^[a-z]:/’ , $path , $matches );
$startWithLetterDir = isset( $matches [ 0 ]) ? $matches [ 0 ] : false ;
// Get and filter empty sub paths
$subPaths = array_filter ( explode ( DIRECTORY_SEPARATOR , $path ), ‘mb_strlen’ );

$absolutes = [];
foreach ( $subPaths as $subPath ) if ( ‘.’ === $subPath ) continue;
>
// if $startWithSeparator is false
// and $startWithLetterDir
// and (absolutes is empty or all previous values are ..)
// save absolute cause that’s a relative and we can’t deal with that and just forget that we want go up
if ( ‘..’ === $subPath
&& ! $startWithSeparator
&& ! $startWithLetterDir
&& empty( array_filter ( $absolutes , function ( $value ) < return !( '..' === $value ); >))
) $absolutes [] = $subPath ;
continue;
>
if ( ‘..’ === $subPath ) array_pop ( $absolutes );
continue;
>
$absolutes [] = $subPath ;
>

return
(( $startWithSeparator ? DIRECTORY_SEPARATOR : $startWithLetterDir ) ?
$startWithLetterDir . DIRECTORY_SEPARATOR : »
). implode ( DIRECTORY_SEPARATOR , $absolutes );
>

/**
* Examples
*
* echo Path::getAbsolute(‘/one/two/../two/./three/../../two’); => /one/two
* echo Path::getAbsolute(‘../one/two/../two/./three/../../two’); => ../one/two
* echo Path::getAbsolute(‘../.././../one/two/../two/./three/../../two’); => ../../../one/two
* echo Path::getAbsolute(‘../././../one/two/../two/./three/../../two’); => ../../one/two
* echo Path::getAbsolute(‘/../one/two/../two/./three/../../two’); => /one/two
* echo Path::getAbsolute(‘/../../one/two/../two/./three/../../two’); => /one/two
* echo Path::getAbsolute(‘c:\.\..\one\two\..\two\.\three\..\..\two’); => c:/one/two
*
*/
>

Needed a method to normalize a virtual path that could handle .. references that go beyond the initial folder reference. So I created the following.

function normalizePath ( $path )
$parts = array(); // Array to build a new path from the good parts
$path = str_replace ( ‘\\’ , ‘/’ , $path ); // Replace backslashes with forwardslashes
$path = preg_replace ( ‘/\/+/’ , ‘/’ , $path ); // Combine multiple slashes into a single slash
$segments = explode ( ‘/’ , $path ); // Collect path segments
$test = » ; // Initialize testing variable
foreach( $segments as $segment )
if( $segment != ‘.’ )
$test = array_pop ( $parts );
if( is_null ( $test ))
$parts [] = $segment ;
else if( $segment == ‘..’ )
if( $test == ‘..’ )
$parts [] = $test ;

if( $test == ‘..’ || $test == » )
$parts [] = $segment ;
>
else
$parts [] = $test ;
$parts [] = $segment ;
>
>
>
return implode ( ‘/’ , $parts );
>
?>

Will convert /path/to/test/.././..//..///..///../one/two/../three/filename
to ../../one/three/filename

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.

The suggestion on the realpath man page is to look for an existing parent directory. Here is an example:
function resolvePath ( $path ) if( DIRECTORY_SEPARATOR !== ‘/’ ) $path = str_replace ( DIRECTORY_SEPARATOR , ‘/’ , $path );
>
$search = explode ( ‘/’ , $path );
$search = array_filter ( $search , function( $part ) return $part !== ‘.’ ;
>);
$append = array();
$match = false ;
while( count ( $search ) > 0 ) $match = realpath ( implode ( ‘/’ , $search ));
if( $match !== false ) break;
>
array_unshift ( $append , array_pop ( $search ));
>;
if( $match === false ) $match = getcwd ();
>
if( count ( $append ) > 0 ) $match .= DIRECTORY_SEPARATOR . implode ( DIRECTORY_SEPARATOR , $append );
>
return $match ;
>
?>

The result will retrieve absolute path for non-existing relative path. Even if a path does not exists, there should be existing directory somewhere, for which the realpath could be obtained. If this is not within the relative path (i.e. even current working directory does not exists), getcwd() will retrieve absolute path, so some absolute path is returned (although in that case the PHP process could have huge problems).

Note: If you use this to check if a file exists, it’s path will be cached, and returns true even if the file is removed (use file_exists instead).

Источник

Get Current Directory Name and Path in PHP

Get Current Directory Name and Path in PHP

  1. Use the getcwd() Function to Get the Current Directory Name in PHP
  2. Use the dirname() Function to Get the Current Directory Name in PHP
  3. Use the basename() Function to Get the Current Directory Name in PHP

This article will introduce a few methods to get the current working directory name in PHP.

Use the getcwd() Function to Get the Current Directory Name in PHP

The getcwd() function gives the current working directory. The returned value is a string on success.

The function does not take any parameters. The function returns false in case of failure.

Let’s consider the following directory structure.

├── var │ └── www │ └── html | └──project | └──index.php 

The PHP file lies inside the project directory. The getcwd() function will return the name of the current working directory, which is project .

We can use the echo function to display the content of the function. We can see in the output section that the getcwd() function returns the current working directory with its path.

Use the dirname() Function to Get the Current Directory Name in PHP

We can also use the dirname() function to get the current directory name in PHP. The function returns the path of the parent directory.

It accepts two parameters where the first one is the path and the second one is levels. Levels indicate the number of directories to move up.

Finally, we can use the __FILE__ magic constants in the dirname() function to get the name of the current directory. The __FILE__ constant returns the full path of the current file along with the file name.

We can demonstrate these constants and the function in the above directory structure. For example, we get the following result when we echo the __FILE__ constant from the index.php file.

/var/www/html/project/index.php 

For example, write the dirname() function in the index.php file with the __FILE__ constant as the parameter.

In this way, we can get the current working directory name in PHP.

Use the basename() Function to Get the Current Directory Name in PHP

We can use the basename() function to get the current working directory name without the path in PHP. We can apply this function with the result of the above two functions.

The basename() function returns the name of the base file or folder from the given path. For example, if the path provided is /var/www/html/project , the output will be project .

For example, use the functions dirname(__FILE__) and getcwd() as the parameters for the basename() function. In this way, we can get the current working directory name in PHP.

echo basename(dirname(__FILE__))."
"
;
echo basename(getcwd())."\n";

Subodh is a proactive software engineer, specialized in fintech industry and a writer who loves to express his software development learnings and set of skills through blogs and articles.

Related Article — PHP File

Related Article — PHP Directory

Copyright © 2023. All right reserved

Источник

Читайте также:  Bootstrap css and bootstrap min css
Оцените статью