Recommend this page to a friend! |
Classes of Christian Vigh | PHP Path Windows and Linux Utilities | README.md | Download |
|
DownloadINTRODUCTIONThe 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. FEATURESUsing the Path class functions, you can :
REFERENCEAppendDS
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. ContainsDirectory
Checks if $path contains the specified directory. This function takes into account the case-sensitiveness of the host platform for filenames. ContainsExtension
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. Equals
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, Filename, Dirname, Basename
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. FileModeToString
Returns a string corresponding to the specified file mode ($mode), in the style supplied by the Unix ls -l command. Find
Implements a Unix find-like utility for finding files in a heirarchy. $path parameterStarting path for the directory tree traversal. $name parameterFile 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. $find_options parameterA combination of the following flags that influence how the search is performed and how the results are returned :
Additional constant exists :
$options parameterBoolean 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' :
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 :
$callback parameterSpecifies a callback function that will be used to select a found file or not. The callback function must have the following signature :
Where :
Return valueWhen 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 :
GetTempDirectory
Returns one of the following values (in the order below) :
HasExtension
Checks if a path has one of the extensions specified by $extension\_list. The parameters are the following :
IsAbolute
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. IsValidFilename
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. IsValidUnixFilename
Checks if the specified path is a valid Unix filename. See the IsValidFilename function for additional information. IsValidWindowsFilename
Checks if the specified path is a valid Windows filename. See the IsValidFilename function for additional information. Matches
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"*. MkDir
Creates a directory using the specified access mode, user and group names. The parameters are the following :
MkFile
Creates a file using the specified access mode, user and group names. The parameters are the following :
NextUniqueFilename
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 :
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 :
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 :
will return "file.0001.txt".
PushDirectory
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. 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. PrependExtension
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 :
Quote
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.
RealPath
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. ReplaceExt
Replaces the extension of $file with $newext. If $newext is null or equal to ".", the file extension will be removed. ToCygwin
Converts a Windows path to a Cygwin-compatible one ; for example :
will be converted to :
On Unix platforms, the supplied path will be returned unchanged. ToUnix
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. ToWindows
Converts a Unix-style path to a Windows-style path. ToHost
Converts a path to the notation used by the current host (Unix or Windows). WhereIs
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. |