Skip to content
forked from lazervel/path

The PHP Path provides utlities help with handling or manipulating file and directory path.

License

Notifications You must be signed in to change notification settings

codingrafel/path

This branch is 4 commits behind lazervel/path:main.

Folders and files

NameName
Last commit message
Last commit date

Latest commit

5537b6c · Nov 17, 2024
Nov 17, 2024
Nov 15, 2024
Nov 15, 2024
Oct 9, 2024
Oct 29, 2024
Oct 9, 2024
Nov 15, 2024
Nov 15, 2024
Nov 15, 2024
Oct 9, 2024
Oct 25, 2024
Oct 7, 2024
Nov 17, 2024
Oct 9, 2024
Nov 15, 2024

Repository files navigation

PHP Path

PHP Path Help with handling or manipulating file and directory path.

Banner

Total Downloads Latest Version Contributors Repository Size

Composer Installation

Installation is super-easy via Composer

composer require filesys/path

or add it by hand to your composer.json file.

The Features of PHP Path

use Path\Path;
require 'vendor/autoload.php';

Path::basename($path[, $suffix])

For example, on POSIX:

Path::basename('C:\\xampp\\htdocs\\example.html');
// Returns: 'example.html'

Path::basename('C:\\xampp\\htdocs\\example.html', '.html');
// Returns: 'example'

On Windows:

Path::basename('/home/local/user/example.html');
// Returns: 'example.html'

Path::basename('/home/local/user/example.html', '.html');
// Returns: 'example'

Path::canonicalize($path)

For example, on POSIX:

Path::canonicalize('C:\\XamPP\\HtDocS\\DatA\\comPoseR.jSon');
// Returns: 'C:\\xampp\\htdocs\\data\\composer.json'

On Windows:

Path::canonicalize('/path/composer.json');
// Returns: 'G:\\path\\composer.json'

Path::changeExt($path, $newExt)

Path::changeExt('/foo/bar/baz/asdf/quux.html', '.php');
// Returns: '/foo/bar/baz/asdf/quux.php'

Path::changeExt('/foo/bar/baz/asdf/vector.gif', 'svg');
// Returns: '/foo/bar/baz/asdf/vector.svg'

Path::combine($paths, $names)

For example, on POSIX:

Path::combine(['/xampp/htdocs'], ['example.html', 'foo.txt']);
// Returns: ['/xampp/htdocs/example.html', '/xampp/htdocs/foo.txt']

Path::combine(['/xampp/htdocs'], ['example.html']);
// Returns: ['/xampp/htdocs/example.html']

Path::combine(['/xampp/htdocs', '/path'], ['example.html']);
// Returns: ['/xampp/htdocs/example.html', '/path/example.html']

Path::combine(['/xampp/htdocs', '/path'], ['example.html', 'foot.txt', '.env']);
// Returns: ['/xampp/htdocs/example.html', '/xampp/htdocs/foot.txt', '/xampp/htdocs/.env', '\path\example.html', '\path\foot.txt', '\path\.env']

On Windows:

Path::combine(['C:\\xampp\\htdocs'], ['example.html', 'foo.txt']);
// Returns: ['C:\\xampp\\htdocs\\example.html', 'C:\\xampp\\htdocs\\foo.txt']

Path::combine(['C:\\xampp\\htdocs'], ['example.html']);
// Returns: ['C:\\xampp\\htdocs\\example.html']

Path::combine(['C:\\xampp\\htdocs', '\\path'], ['example.html']);
// Returns: ['C:\\xampp\\htdocs\\example.html', '\\path\\example.html']

Path::combine(['C:\\xampp\\htdocs', '\\path'], ['example.html', 'foot.txt', '.env']);
// Returns: ['C:\\xampp\\htdocs\\example.html', 'C:\\xampp\\htdocs\\foot.txt', 'C:\\xampp\\htdocs\\.env', '\\path\\example.html', '\\path\\foot.txt', '\\path\\.env']

Path::checkLength($path)

// Check maximum path length on your system use \PHP_MAXPATHLEN constant.
Path::checkLength('your-path');

// Returns: if given path of length are valid so return (void) otherwise throwing RTException Error.
// PHP Fatal error:  Uncaught Path\Exception\RTException: Invalid path because path length exceeds [2048] characters.

Throwing Error

Path\Exception\RTException: Invalid path because path length exceeds [2048] characters.

Path::delimiter

Provides the platform-specific path delimiter:

  • ; for Windows
  • : for POSIX

For example, on POSIX:

echo getenv('PATH');
// Prints: '/usr/bin:/bin:/usr/sbin:/sbin:/usr/local/bin'

explode(Path::delimiter, getenv('PATH'));
// Returns: ['/usr/bin', '/bin', '/usr/sbin', '/sbin', '/usr/local/bin']

On Windows:

echo getenv('PATH');
// Prints: 'C:\Windows\system32;C:\Windows;C:\Program Files\node\'

explode(Path::delimiter, getenv('PATH'));
// Returns ['C:\\Windows\\system32', 'C:\\Windows', 'C:\\Program Files\\node\\']

Path::dirname($path[, $suffix, $levels])

For example, on POSIX:

Path::dirname('/foo/bar/baz/asdf/quux\\abcd\\xyz');
// Returns: '/foo/bar/baz/asdf'

On Windows:

Path::dirname('/foo/bar/baz/asdf/quux');
// Returns: '/foo/bar/baz/asdf'

Path::dirname('/foo/bar/baz/asdf/quux\\abcd\\xyz');
// Returns: '/foo/bar/baz/asdf/quux\abcd'

Path::extname($path)

Path::extname('C:\\xampp\\htdocs\\example.html');
// Returns: '.html'

Path::extname('index.coffee.md');
// Returns: '.md'

Path::extname('index.');
// Returns: '.'

Path::extname('index');
// Returns: ''

Path::extname('.index');
// Returns: '.index'

Path::extname('C:\\xampp\\htdocs\\example.md');
// Returns: '.md' 

Path::filename($path)

Path::filename('/foo/bar/baz/asdf/quux.html');
// Returns: 'quux.html'

Path::filename('example.txt');

Path::filename('/');
// Returns: ''
// Returns: 'example.txt'

Path::filename('example');
// Returns: 'example'

Path::filename('C:\\path\\dir\\file.txt');
// Returns: 'file.txt'

Path::format($pathObject)

For example, on POSIX:

// If `dir`, `root` and `base` are provided,
// `${dir}${Path::sep}${base}`
// will be returned. `root` is ignored.
Path::format([
  'root' => '/ignored',
  'dir' => '/home/user/dir',
  'base' => 'file.txt',
]);
// Returns: '/home/user/dir/file.txt'

// `root` will be used if `dir` is not specified.
// If only `root` is provided or `dir` is equal to `root` then the
// platform separator will not be included. `ext` will be ignored.
Path::format([
  'root' => '/',
  'base' => 'file.txt',
  'ext' => 'ignored',
]);
// Returns: '/file.txt'

// `name` + `ext` will be used if `base` is not specified.
Path::format([
  'root' => '/',
  'name' => 'file',
  'ext' => '.txt',
]);
// Returns: '/file.txt'

// The dot will be added if it is not specified in `ext`.
Path::format([
  'root' => '/',
  'name' => 'file',
  'ext' => 'txt',
]);
// Returns: '/file.txt'

On Windows:

Path::format([
  'dir' => 'C:\\path\\dir',
  'base' => 'file.txt',
]);
// Returns: 'C:\\path\\dir\\file.txt'

Path::getcwd()

For example, on POSIX:

// If the current working directory is /xampp/htdocs,
Path::getcwd(); // Returns: /xampp/htdocs

On Windows:

// If the current working directory is C:\\xampp\\htdocs,
// returns with drive LIKE (eg: C:,D:,F: etc.)
Path::getcwd(); // Returns: C:\\xampp\\htdocs

Path::hasExt($path)

Path::hasExt('/foo/bar/baz/asdf/vector.png', ['.gif', '.jpg', '.png']); // Returns: true
Path::hasExt('/foo/bar/baz/asdf/vector.gif', '.gif');                   // Returns: true
Path::hasExt('/foo/bar/baz/asdf/vector.gif', 'gif');                    // Returns: true
Path::hasExt('/foo/bar/baz/asdf/vector.gif', ['gif', 'jpeg', 'png']);   // Returns: true

Path::hasExt('/foo/bar/baz/asdf/vector.pdf', ['.gif', '.jpg', '.png']); // Returns: false
Path::hasExt('/foo/bar/baz/asdf/vector.gif', '.svg');                   // Returns: false
Path::hasExt('/foo/bar/baz/asdf/vector.gif', 'png');                    // Returns: false
Path::hasExt('/foo/bar/baz/asdf/vector.gif', ['svg', 'jpeg', 'png']);   // Returns: false

Path::info()

For example, on POSIX:

Path::info('/home/local/user/example.html');
// Returns: stdClass Object (
//   [dirname] => /home/local/user
//   [basename] => example.html
//   [extension] => html
//   [filename] => example
//   [root] => /
// )

On Windows:

Path::info('C:\\xampp\\htdocs\\path\\Path.php');
// Returns: stdClass Object (
//   [dirname] => C:/xampp/htdocs/path
//   [basename] => Path.php
//   [extension] => php
//   [filename] => Path
//   [root] => C:\
// )

Path::isAbsolute($path)

For example, on POSIX:

Path::isAbsolute('/foo/bar'); // Returns: true
Path::isAbsolute('/baz/..');  // Returns: true
Path::isAbsolute('qux/');     // Returns: false
Path::isAbsolute('.');        // Returns: false

On Windows:

Path::isAbsolute('//server');    // Returns: true
Path::isAbsolute('\\\\server');  // Returns: true
Path::isAbsolute('C:/foo/..');   // Returns: true
Path::isAbsolute('C:\\foo\\..'); // Returns: true
Path::isAbsolute('bar\\baz');    // Returns: false
Path::isAbsolute('bar/baz');     // Returns: false
Path::isAbsolute('.');           // Returns: false

Path::isLocal($path)

Path::isLocal('C:Users\JohnDoe\Documents\file.txt');  // Returns: 'false'
Path::isLocal('//home/user/file.txt');                // Returns: 'false'
Path::isLocal('C:\Program Files\file//file.txt');     // Returns: 'false'
Path::isLocal('C:/Windows\\System32');                // Returns: 'false'
Path::isLocal('D:\\Data\report.pdf');                 // Returns: 'false'
Path::isLocal('C:\Users\JohnDoe\Documents\file.txt'); // Returns: 'true'
Path::isLocal('D:\Projects\Code\index.html');         // Returns: 'true'
Path::isLocal('/home/user/documents/report.pdf');     // Returns: 'true'
Path::isLocal('\\ServerName\SharedFolder\image.png'); // Returns: 'true'
Path::isLocal('E:\Music\Rock\song.mp3');              // Returns: 'true'

Path::isURIPath($path)

Support: working only Windows:

Path::isURIPath('//home/local/user/'); // Returns: true
Path::isURIPath('//home/local');       // Returns: true
Path::isURIPath('//home/local/');      // Returns: true
Path::isURIPath('/server/foo/');       // Returns: false
Path::isURIPath('D:/');                // Returns: false
Path::isURIPath('//home/');            // Returns: false
Path::isURIPath('C:/xampp/htdocs/');   // Returns: false

Path::join([...$paths])

Path::join('/foo', 'bar', 'baz/asdf', 'quux', '..');
// Returns: '/foo/bar/baz/asdf'

Path::join('foo', [], 'bar');
// Throws TypeError: Path\Path::join(): Argument #2 must be of type string, array given.

Path::localBase($paths)

// Temporary Unavailable

Path::normalize($path)

For example, on POSIX:

Path::normalize('/foo/bar//baz/asdf/quux/..');
// Returns: '/foo/bar/baz/asdf'

On Windows:

Path::normalize('C:\\temp\\\\foo\\bar\\..\\');
// Returns: 'C:\\temp\\foo\\'

Since Windows recognizes multiple path separators, both separators will be replaced by instances of the Windows preferred separator (\):

Path::win32::normalize('C:////temp\\\\/\\/\\/foo/bar');
// Returns: 'C:\\temp\\foo\\bar'

Path::optimize($path)

Path::parse($path)

For example, on POSIX:

Path::parse('/home/user/dir/file.txt');
// Returns:
// [
//   'root' => '/',
//   'dir' => '/home/user/dir',
//   'base' => 'file.txt',
//   'ext' => '.txt',
//   'name' => 'file'
// ]
┌─────────────────────┬────────────┐
│          dir        │    base    │
├──────┬              ├──────┬─────┤
│ root │              │ name │ ext │
"  /    home/user/dir / file  .txt "
└──────┴──────────────┴──────┴─────┘
(All spaces in the "" line should be ignored. They are purely for formatting.)

On Windows:

path.parse('C:\\path\\dir\\file.txt');
// Returns:
// [
//   'root' => 'C:\\',
//   'dir' => 'C:\\path\\dir',
//   'base' => 'file.txt',
//   'ext' => '.txt',
//   'name' => 'file'
// ]
┌─────────────────────┬────────────┐
│          dir        │    base    │
├──────┬              ├──────┬─────┤
│ root │              │ name │ ext │
" C:\      path\dir   \ file  .txt "
└──────┴──────────────┴──────┴─────┘
(All spaces in the "" line should be ignored. They are purely for formatting.)

Path::pathname($path)

For example, on POSIX:

Path::pathname('//var/www/httpdocs/config/config.yml');
// Returns: '/var/www/httpdocs/config/config.yml'

Path::pathname('C:////temp\\\\/\\/\\/foo/bar');
// Returns: 'C:/temp\foo/bar'

Path::pathname('/');
// Returns: '/'

Path::pathname('/var/www/httpdocs/config/config.yml');
// Returns: '/var/www/httpdocs/config/config.yml'

On Windows:

// Handle Network Path, Here network path are '\\\\var\\www'
Path::pathname('\\\\var\\www\\httpdocs\\config\\config.yml');
// Returns: '\\httpdocs\\config\\config.yml'

Path::pathname('C:////temp\\\\/\\/\\/foo/bar');
// Returns: '\\temp\\foo\\bar'

Path::pathname('\\var\\www\\httpdocs\\config\\config.yml');
// Returns: '\\var\\www\\httpdocs\\config\\config.yml'

Path::pathname('\\\\var\\www\\');
// Returns: '\\'

Path::pathname('C:');
// Returns: ''

Path::pathname('C:\\');
// Returns: '\\'

Path::pathname('\\\\var\\www');
// Returns: ''

Path::pathname('G:var\\www\\httpdocs\\config\\config.yml');
// Returns: 'var\\www\\httpdocs\\config\\config.yml'

Since Windows recognizes multiple path separators, both separators will be replaced by instances of the Windows preferred separator (\):

Path::pathToURL($path, $origin[, ?$query, ?$hash])

Notice: Don't use syntax Path::win32::pathToURL() or Path::posix::pathToURL(), This a common bugs. but don't worry we fix this bugs to next expected version [v10.2.0].

For example, on POSIX:

Path::pathToURL('server/auth/client', 'https://www.example.com', 'id=1');
// Returns: 'https://www.example.com/server/auth/client?id=1'

Path::pathToURL('server/auth/client', 'https://www.example.com');
// Returns: 'https://www.example.com/server/auth/client'

Path::pathToURL('server/auth/client', 'https://www.example.com', '?id=1', '#root');
// Returns: 'https://www.example.com/server/auth/client?id=1#root'

Path::pathToURL('server/auth/client', 'https://www.example.com', '?id=1', 'root');
// Returns: 'https://www.example.com/server/auth/client?id=1#root'

On Windows:

Path::pathToURL('G:\\server\\auth\\client', 'https://www.example.com', 'id=1');
// Returns: 'https://www.example.com/server/auth/client?id=1'

Path::pathToURL('G:\\server\\auth\\client', 'https://www.example.com');
// Returns: 'https://www.example.com/server/auth/client'

Path::pathToURL('G:\\server\\auth\\client', 'https://www.example.com', '?id=1', '#root');
// Returns: 'https://www.example.com/server/auth/client?id=1#root'

Path::pathToURL('G:\\server\\auth\\client', 'https://www.example.com', '?id=1', 'root');
// Returns: 'https://www.example.com/server/auth/client?id=1#root'

Path::posix

The Path::posix property provides access to POSIX specific implementations of the Path methods.

The API is accessible via Path\Path::posix or Path\Linux\Linux::class.

Path::relative($from, $to)

For example, on POSIX:

Path::relative('/data/orandea/test/aaa', '/data/orandea/impl/bbb');
// Returns: '../../impl/bbb'

On Windows:

Path::relative('C:\\orandea\\test\\aaa', 'C:\\orandea\\impl\\bbb');
// Returns: '..\\..\\impl\\bbb'

Path::removeExt($path)

Path::removeExt('/var/www/web.php');
// Returns: '/var/www/web'

Path::removeExt('.env.local');
// Returns: '.env'

Path::removeExt('.html');
// Returns: '' bugs detected

Path::removeExt('file.txt');
// Returns 'file'

Path::removeExt('G:/path/.github');
// Returns: 'G:/path/' bugs detected

Path::resolve($path)

For example, on POSIX:

Path::resolve('/foo/bar', './baz');
// Returns: '/foo/bar/baz'

Path::resolve('/foo/bar', '/tmp/file/');
// Returns: '/tmp/file'

Path::resolve('wwwroot', 'static_files/png/', '../gif/image.gif');
// If the current working directory is /home/myself/node,
// this returns '/home/myself/node/wwwroot/static_files/gif/image.gif'

On Windows:

Path::resolve('/foo/bar', './baz');
// Returns: 'G:\\foo\\bar\\baz'

Path::resolve('/foo/bar', '/tmp/file/');
// Returns: 'G:\\tmp\\file'

Path::resolve('wwwroot', 'static_files/png/', '../gif/image.gif');
// If the current working directory is C:\\xampp\\htdocs/,
// this returns 'C:\\xampp\\htdocs\\wwwroot\\static_files\\gif\\image.gif'

Path::rootname($path)

For example, on POSIX:

Path::rootname('C:\\xampp\\htdocs\\');
// Returns: ''

Path::rootname('/var/ww/httpdocs');
// Returns: '/'

Path::rootname('C:\\');
// Returns: ''

Path::rootname('G:');
// Returns: ''

Path::rootname('//var/www/httpdocs');
// Returns: '/'

On Windows:

Path::rootname('C:\\xampp\\htdocs\\');
// Returns: 'C:\\'

Path::rootname('/var/ww/httpdocs');
// Returns: '\\'

Path::rootname('C:\\');
// Returns: 'C:\\'

Path::rootname('G:');
// Returns: 'G:'

Path::rootname('//var/www/httpdocs');
// Returns: '\\\\var\\www\\'

Path::sep

For example, on POSIX:

explode(Path::sep, 'foo/bar/baz');
// Returns: ['foo', 'bar', 'baz']

On Windows:

explode(Path::sep, 'foo\\bar\\baz');
// Returns: ['foo', 'bar', 'baz']

On Windows, both the forward slash (/) and backward slash (\) are accepted as path segment separators; however, the Path methods only add backward slashes (\).

Path::tmp($path)

For example, on POSIX:

// Path::tmp suffix random tmp name in given path value.
Path::tmp('foot/bar/baz');
// Returns: 'foot/bar/.!!/.!HyZq'
// Returns: 'foot/bar/.!!/.!XTfs'
// Returns: 'foot/bar/.!!/.!C80D'

On Windows:

// Path::tmp suffix random tmp name in given path value.
Path::tmp('foot\\bar\\baz');
// Returns: 'foot\\bar\\.!!\\.!RBDZ'
// Returns: 'foot\\bar\\.!!\\.!NPia'
// Returns: 'foot\\bar\\.!!\\.!0Kbx'

Path::toNamespacedPath($path)

Path::toNamespacedPath('\\foo\\bar/baz\\asdfquux\\abcd\\xyz');
// Returns: '\\\\?\\G:\\foo\\bar\\baz\\asdfquux\\abcd\\xyz'

Path::toNamespacedPath('//foo\\bar/baz\\asdfquux\\abcd\\xyz');
// Returns: '\\\\?\\UNC\\foo\\bar\\baz\\asdfquux\\abcd\\xyz'

Path::UrlToPath($url)

For example, on POSIX:

Path::UrlToPath('https://www.example.com/server/auth/client?id=1');
// Returns: 'G:\\server\\auth\\client'

Path::UrlToPath('https://www.example.com/server/auth/client');
// Returns: 'G:\\server\\auth\\client'

Path::UrlToPath('https://www.example.com/server/auth/client?id=1#root');
// Returns: 'G:\\server\\auth\\client'

On Windows:

Path::UrlToPath('https://www.example.com/server/auth/client?id=1');
// Returns: '/server/auth/client'

Path::UrlToPath('https://www.example.com/server/auth/client');
// Returns: '/server/auth/client'

Path::UrlToPath('https://www.example.com/server/auth/client?id=1#root');
// Returns: '/server/auth/client'

Path::win32

The Path::win32 property provides access to Windows specific implementations of the Path methods.

The API is accessible via Path\Path::win32 or Path\Win32\Win32::class.

Resources

About

The PHP Path provides utlities help with handling or manipulating file and directory path.

Resources

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PHP 100.0%