Recommend this page to a friend! |
Download |
Info | Example | Files | Install with Composer | Download | Reputation | Support forum | Blog | Links |
Ratings | Unique User Downloads | Download Rankings | ||||
64% | Total: 174 | All time: 8,789 This week: 67 |
Version | License | PHP version | Categories | |||
path-utilities 1.0.17 | BSD License | 5.6 | PHP 5, Files and Folders, Unix, Windows |
Description | Author | |||
This class can manage file and folder paths in Windows and Linux or other Unix systems. Innovation Award
|
<?php
|
The Path class contains static functions for handling paths and, more generally, for relieving you from the burden of handling platform-specifics issues related to paths, such as the ones you ay find when switching from Windows/Unix platforms.
Using the Path class functions, you can :
$newpath = Path::AppendDS ( $path ) ;
Appends a directory separator to the specified path, if not present.
This function takes into account the difference in directory separator between the Windows and Unix platforms.
$status = Path::ContainsDirectory ( $path, $directory ) ;
Checks if $path contains the specified directory.
This function takes into account the case-sensitiveness of the host platform for filenames.
$status = Path::ContainsExtension ( $path, $extension ) ;
Checks if the specified path contains the specified extension.
The $extension parameter can be either a single file extension, or an array of file extensions. File extensions must not include a dot.
This function takes into account the case-sensitiveness of the host platform for filenames.
$status = Path::Equals ( $path1, $path2 ) ;
Checks if two paths are equal. Both path can be either relative or absolute.
This function takes into account the case-sensitiveness of the host platform for filenames.
$extension = Path::Extension ( $path ) ;
$filename = Path::Extension ( $path ) ;
$dirname = Path::Extension ( $path ) ;
$basename = Path::Extension ( $path ) ;
These functions provide an alternative to the PHP pathinfo() builtin function.
The reason for their existence is that the PATHINFO\_FILENAME constant became available only with PHP 5.2.0.
$mode_string = Path::FileModeToString ( $mode ) ;
Returns a string corresponding to the specified file mode ($mode), in the style supplied by the Unix ls -l command.
$result = Find ( $path, $name = null, $find_options = FIND_OPTIONS_DEFAULT, $options = null, $callback = null ) ;
Implements a Unix find-like utility for finding files in a heirarchy.
Starting path for the directory tree traversal.
File pattern(s) to be searched.
If the pattern starts with the string 're:', then it will be considered as a regular expression enclosed with the character immediately after 're' ; otherwise, the normal Unix file pattern matching rules will be applied.
A combination of the following flags that influence how the search is performed and how the results are returned :
Additional constant exists :
Boolean expression representing the file selection criterias. This can be any valid PHP expression, specifying additional constructs referencing file information fields.
File information fields are enclosed with square brackets, and the values they are compared with must be enclosed with quotes. The following, for example, will search for file greater than 500kb and whose modification time is greater than '2011/12/01' :
"[size] > '500kb' && [mtime] > '2011/12/01'"
Other boolean expressions can be mixed as well.
The available file information fields are the following :
The following constants evaluate to true if the corresponding file mode bit is set :
The following example will select all entries whose size is greater than 300kb, and that have the READ and EXECUTE permissions for the owner user :
"[size] > '300kb' && S_IRUSR && S_IXUSR"
Specifies a callback function that will be used to select a found file or not.
The callback function must have the following signature :
boolean callback ( $fullpath, $filename, $stat_info )
Where :
When the FIND\_OPTION\_FILES\_ONLY flag has been specified, returns an array containing the filenames that have been found.
Otherwise, the return value will be an associative array whose keys are the filenames and values are informations such as stat info and extended stat info. Each item in the returned array have the following entries :
When the FIND\_OPTIONS\_EXTENDED\_STAT\_INFO flag specified, the stat information will contain an extra entry, 'modes', which will contain one boolean entry for eachS\_xx* file mode constant defined :
$path = Path::GetTempDirectory ( ) ;
Returns one of the following values (in the order below) :
$status = HasExtension ( $path, $extension_list, &$found_extension = null, &$original_extension = null ) ;
Checks if a path has one of the extensions specified by $extension\_list.
The parameters are the following :
$status = Path::IsAbsolute ( $path )
Checks if the specified path is absolute. Returns true if the path is absolute, false otherwise.
On Unix systems, a path is absolute if it starts with the '/' character. On Windows systems, a path is absolute if its starts either with :
This function takes into account the specificities of the Windows/Unix platforms.
$status = Path::IsValidFilename ( $filename, $is_dir = false ) ;
Checks if the specified filename is a valid filename. The type of validation depends on whether we are running on a Unix or Windows system.
The parameters are the following :
This function only checks that the specified path is sintactically correct.
Checking for a directory is only performed on a syntax basis ; no filesystem checking is performed.
$status = Path::IsValidUnixFilename ( $filename, $is_dir = false ) ;
Checks if the specified path is a valid Unix filename. See the IsValidFilename function for additional information.
$status = Path::IsValidWindowsFilename ( $filename, $is_dir = false ) ;
Checks if the specified path is a valid Windows filename. See the IsValidFilename function for additional information.
$status = Matches ( $file, $pattern, $case_sensitive = false ) ;
Matches a filename against a file mask. The pattern is in the style of Msdos or Unix file-matching patterns, and can contain the following special elements :
Directory separators act as stop-characters ; for example, "file\.txt"will match"file.001.txt"but not"file/001/file.txt"*.
$status = Path::MkDir ( $path, $mode = 0755, $recursive = false, $user = false, $group = false ) ;
Creates a directory using the specified access mode, user and group names.
The parameters are the following :
$status = Path::MkFile ( $path, $mode = 0755, $recursive = false, $user = false, $group = false ) ;
Creates a file using the specified access mode, user and group names.
The parameters are the following :
$filename = Path::NextUniqueFilename ( $file_specifier, $specifier = 's' ) ;
Generates a unique filename based on the specified $file_specifier parameter. The unicity of the filename is determined by a unique integer id, which will replace the sprintf()-like format specified in $file_specifier.
The following example :
$filename = Path::NextUniqueFilename ( 'file.%s.txt' ) ;
will return "file.1.txt" if the current directory (ie, the one for file "file.*.txt") does not contain any filename having this naming scheme.
If the directory already contains several files having this naming scheme, say "file.1.txt" through "file.99.txt", then the next call to NextUniqueFilename() will return "file.100.txt".
If not format specifier has been found in the $file_specifier string, then ".%s" will be appended to the supplied value ; so, the following call :
$filename = Path::NextUniqueFilename ( 'file.txt' ) ;
will return "file.txt.1" if no files matched the mask "file.txt.*".
The $specifier parameter allows you to specify a letter other than "s" for a format specifier.
Format specifiers can include any width options, such as for sprintf() ; the following example :
$filename = Path::NextUniqueFilename ( 'file.%04s.txt' ) ;
will return "file.0001.txt".
Path::PushDirectory ( $directory = false ) ;
Pushes the specified directory onto the directory stack.
If the $directory parameter is not specified, the current working directory will be pushed onto the stack.
The PushDirectory() method does not change the current working directory ; it is the caller responsibility to perform a call to the PHP builtin chdir() function.
$status = Path::PopDirectory ( ) ;
Pops the last pushed directory from the directory stack and set it to be the current working directory if the directory stack is not empty.
When the directory stack is empty, PopDirectory() returns false.
$path = Path::PrependExtension ( $path, $extension ) ;
Prepends an extension before the specified path extension. If the path does not contain any extension, it will simply be appended.
The parameters are the following :
$quoted = Path::Quote ( $path, $force = false ) ;
Put quotes around a filename if it contains spaces or quotes (internal quotes will be escaped). Quotes are put only if needed, unless the $force parameter is set to true.
$path = Path::RealPath ( $path, $use_cwd = false ) ;
This function is a true alternative to the builtin realpath() function ; it works even if the specified path exists.
If you use this function on a stream wrapper that just implements a way to reference a particular part of a filesystem, the RealPath function will check if the strea options contain an entry named root and will use that root to build the final path.
When the $use\_cwd parameter is true, the current working directory will be used for building the absolute path.
When false, it will be the directory of the currently running script.
If specified as a string, it will be considered as the current working directory for relative paths.
Note : Don't try to get the absolute path of a relative path within a specified drive on Windows. Windows has a notion of per-drive current working directory ; however PHP does not provide any support for retrieving the current working directory for a specific drive.
$newfile = Path::ReplaceExtension ( $file, $newext ) ;
Replaces the extension of $file with $newext. If $newext is null or equal to ".", the file extension will be removed.
$result = Path::ToCygWin ( $path ) ;
Converts a Windows path to a Cygwin-compatible one ; for example :
C:\Temp\sample.txt
will be converted to :
/cygdrive/c/temp/sample.txt
On Unix platforms, the supplied path will be returned unchanged.
$result = ToUnix ( $path, $strip_drive_letter = false ) ;
converts a windows-style path to unix ; backslashes are replaced by slashes, and the drive letter (along with the following semicolon) is removed from the string if the $strip\_drive\_letter parameter is true.
$result = ToWindows ( $path ) ;
Converts a Unix-style path to a Windows-style path.
$result = ToHost ( $path, $append_trailing_separator = false ) ;
Converts a path to the notation used by the current host (Unix or Windows).
$realpath = Path::WhereIs ( $command ) ;
Tries to locate the real path of a command, by using the PATH environment variable.
On Windows systems, the PATHEXT environment variable is also used to determine which extensions are for executables, in case the supplied command name does not contain any extension.
Files (4) |
File | Role | Description |
---|---|---|
example.find.php | Example | Example script |
example.realpath.php | Example | Example script |
Path.phpclass | Class | Class source |
README.md | Doc. | Documentation |
The PHP Classes site has supported package installation using the Composer tool since 2013, as you may verify by reading this instructions page. |
Install with Composer |
Version Control | Reuses | Unique User Downloads | Download Rankings | ||||||||||||||||
100% | 1 |
|
|
User Ratings | ||||||||||||||||||||||||||||||
|
Applications that use this package |
If you know an application of this package, send a message to the author to add a link here.
Other classes that need this package |
Class | Why it is needed | Dependency |
---|---|---|
PHP Wamp Server Installation Manager | dependency | Optional |