Provides utility methods for manipulating files.
More...
List of all members.
Static Public Member Functions |
static | copyRecursive ($source, $target) |
| Copy a file or folder recursively.
|
static | createWritablePath ($path) |
| Create the given path and make it writable.
|
static | deleteRecursive ($path) |
| Delete a directory (and all its contents) recursively.
|
static | getMimeType ($file) |
| Detect the mime-type of the given file.
|
static | md5Recursive ($path, $basePath=null, array $exclude=null) |
| Calculate the md5sum of a directory (and all its contents) recursively.
|
Detailed Description
Provides utility methods for manipulating files.
- Copyright:
- 2011-2012 Perforce Software. All rights reserved
- License:
- Please see LICENSE.txt in top-level folder of this distribution.
- Version:
- 2012.2/486814
Member Function Documentation
static P4Cms_FileUtility::copyRecursive |
( |
$ |
source, |
|
|
$ |
target |
|
) |
| [static] |
Copy a file or folder recursively.
- Parameters:
-
string | $source | the source path to copy. |
string | $target | the target path to copy to. |
- Exceptions:
-
InvalidArgumentException | if the source does not exist or if the target's containing folder doesn't exist |
Exception | if the copy fails |
{
$source = rtrim($source, '/\\');
$target = rtrim($target, '/\\');
if (!file_exists($source)) {
throw new InvalidArgumentException("Cannot copy from non-existent source.");
}
if (!file_exists(dirname($target))) {
throw new InvalidArgumentException("Cannot copy to target with non-existent parent.");
}
$error = "Failed to copy from $source to $target";
if (!is_dir($source)) {
$result = @copy($source, $target);
if (!$result) {
throw new Exception($error);
}
return;
}
if (!@mkdir($target, fileperms($source))) {
throw new Exception($error);
}
$sourceList = new RecursiveIteratorIterator(
new RecursiveDirectoryIterator(
$source,
RecursiveDirectoryIterator::SKIP_DOTS
),
RecursiveIteratorIterator::SELF_FIRST
);
foreach ($sourceList as $item) {
if ($item->isDir()) {
$result = @mkdir($target . '/' . $sourceList->getSubPathName(), fileperms($item->getPathname()));
} else {
$result = @copy($item->getPathname(), $target . '/' . $sourceList->getSubPathName());
}
if (!$result) {
throw new Exception($error);
}
}
}
static P4Cms_FileUtility::createWritablePath |
( |
$ |
path | ) |
[static] |
Create the given path and make it writable.
- Parameters:
-
string | $path | the path to create and make writable. |
{
if (!is_dir($path)) {
if (!@mkdir($path, 0755, true)) {
throw new Exception(
basename($path) . " directory does not exist and could not be created ('$path')."
);
}
}
if (!is_writable($path)) {
if (!@chmod($path, 0755)) {
throw new Exception(
"Unable to make " . basename($path) . " directory writable ('$path')."
);
}
}
}
static P4Cms_FileUtility::deleteRecursive |
( |
$ |
path | ) |
[static] |
Delete a directory (and all its contents) recursively.
- Parameters:
-
string | $path | the path to the directory to remove. |
- Returns:
- bool true on success, false on failure.
{
if (!file_exists($path)) {
return;
}
if (!is_dir($path)) {
throw new InvalidArgumentException(
"Failed to delete path. Path is not a directory."
);
}
$entries = new DirectoryIterator($path);
foreach ($entries as $entry) {
if ($entry->isDir() && $entry->isDot()) {
continue;
}
if ($entry->isFile()) {
@chmod($entry->getPathname(), 0777);
@unlink($entry->getPathname());
} else {
static::deleteRecursive($entry->getPathname());
}
}
@chmod($path, 0777);
return @rmdir($path);
}
static P4Cms_FileUtility::getMimeType |
( |
$ |
file | ) |
[static] |
Detect the mime-type of the given file.
- Parameters:
-
string | $file | the file to detect the mime type of. |
- Exceptions:
-
InvalidArgumentException | if the given file does not exist. |
static P4Cms_FileUtility::md5Recursive |
( |
$ |
path, |
|
|
$ |
basePath = null , |
|
|
array $ |
exclude = null |
|
) |
| [static] |
Calculate the md5sum of a directory (and all its contents) recursively.
- Parameters:
-
string | $path | the path to the directory to examine. |
string | $basePath | the base path for the action |
array | $exclude | a list of file(s) including relative path to exclude |
{
if (!is_dir($path)) {
throw new InvalidArgumentException('Provided path is not a valid directory.');
}
$basePath = ($basePath === null ) ? $path : $basePath;
$path = rtrim($path, '/');
$md5List = array();
$entries = new DirectoryIterator($path);
foreach ($entries as $entry) {
if ($entry->isDir() && $entry->isDot()) {
continue;
}
if ($entry->isFile()) {
if (strpos($path, $basePath) === 0) {
$relativePath = ltrim(substr($path, strlen($basePath)), '/\\');
}
if (P4_Environment::isWindows()) {
$relativePath = str_replace('\\', '/', $relativePath);
}
$filename = $entry->getFilename();
$relativeFile = !empty($relativePath)
? $relativePath . '/' . $filename
: $filename;
if ($exclude === null || !in_array($relativeFile, $exclude)) {
$md5List[] = md5_file($path . '/' . $filename) . ' ' . $relativeFile;
}
} else {
$md5List = array_merge($md5List, static::md5Recursive($entry->getPathname(), $basePath, $exclude));
}
}
return $md5List;
}
The documentation for this class was generated from the following file: