class CRM_Utils_File

class to provide simple static functions for file objects

Methods

static bool
isAscii(string $name)

Given a file name, determine if the file contents make it an ascii file

static bool
isHtml(string $name)

Given a file name, determine if the file contents make it an html file

static bool|NULL
createDir(string $path, bool $abort = TRUE)

Create a directory given a path name, creates parent directories if needed

static 
cleanDir(string $target, bool $rmdir = TRUE, bool $verbose = TRUE)

Delete a directory given a path name, delete children directories and files if needed

static string
concat(array $files, string $delim = '')

Concatenate several files.

static 
copyDir(string $source, string $destination)

No description

static bool
toUtf8(string $name)

Given a file name, recode it (in place!) to UTF-8

static string
addTrailingSlash(string $path, string $slash = NULL)

Appends a slash to the end of a string if it doesn't already end with one

static string
createFakeFile(string $dir, string $contents = 'delete me', string $fileName = NULL)

Save a fake file somewhere

static 
sourceSQLFile(string|NULL $dsn, string $fileName, string $prefix = NULL, bool $dieOnErrors = TRUE)

No description

static 
runSqlQuery(string|NULL $dsn, string $queryString, string $prefix = NULL, bool $dieOnErrors = TRUE)

No description

static string
stripComments(string $string)

Strips comment from a possibly multiline SQL string

static bool
isExtensionSafe($ext)

No description

static bool
isIncludable(string $name)

Determine whether a given file is listed in the PHP include path.

static mixed
cleanFileName($name)

Remove the 32 bit md5 we add to the fileName also remove the unknown tag if we added it.

static string
makeFileName(string $name)

Make a valid file name.

static mixed
duplicate($filePath)

Copies a file

static array
getFilesByExtension(string $path, string $ext)

Get files for the extension.

static 
restrictAccess(string $dir, bool $overwrite = FALSE)

Restrict access to a given directory (by planting there a restrictive .htaccess file)

static 
restrictBrowsing($publicDir)

Restrict remote users from browsing the given directory.

static 
baseFilePath() deprecated

(Deprecated) Create the file-path from which all other internal paths are computed. This implementation determines it as dirname(CIVICRM_TEMPLATE_COMPILEDIR).

static bool
isAbsolute(string $path)

Determine if a path is absolute.

static string
relativeDirectory($directory) deprecated

No description

static string
absoluteDirectory($directory, string $basePath)

No description

static string
relativize($directory, $basePath)

Make a file path relative to some base dir.

static string,
tempnam(string $prefix = 'tmp-')

Create a path to a temporary file which can endure for multiple requests.

static string,
tempdir(string $prefix = 'tmp-')

Create a path to a temporary directory which can endure for multiple requests.

static array(string)
findFiles(string $dir, string $pattern, bool $relative = FALSE)

Search directory tree for files which match a glob pattern.

static bool
isChildPath(string $parent, string $child, bool $checkRealPath = TRUE)

Determine if $child is a sub-directory of $parent

static bool
replaceDir(string $fromDir, string $toDir, bool $verbose = FALSE)

Move $fromDir to $toDir, replacing/deleting any pre-existing content.

static 
formatFile(array $param, string $fileName, array $extraParams = [])

Format file.

static string
getFileURL(string $path, string $fileType, string $url = NULL)

Return formatted file URL, like for image file return image url with image icon

static string
getImageURL(string $imageURL)

Return formatted image icon

static string
resizeImage(string $sourceFile, int $targetWidth, int $targetHeight, string $suffix = "", bool $preserveAspect = TRUE)

Resize an image.

static string
getIconFromMimeType(string $mimeType)

Get file icon class for specific MIME Type

static bool
isValidFileName(string $fileName = NULL)

Is the filename a safe and valid filename passed in from URL

static array
getAcceptableExtensionsForMimeType(string $mimeType = NULL)

Get the extensions that this MimeTpe is for

static string
getExtensionFromPath(string $path)

Get the extension of a file based on its path

Details

at line 48
static bool isAscii(string $name)

Given a file name, determine if the file contents make it an ascii file

Parameters

string $name Name of file.

Return Value

bool true if file is ascii

at line 76
static bool isHtml(string $name)

Given a file name, determine if the file contents make it an html file

Parameters

string $name Name of file.

Return Value

bool true if file is html

at line 110
static bool|NULL createDir(string $path, bool $abort = TRUE)

Create a directory given a path name, creates parent directories if needed

Parameters

string $path The path name.
bool $abort Should we abort or just return an invalid code.

Return Value

bool|NULL NULL: Folder already exists or was not specified. TRUE: Creation succeeded. FALSE: Creation failed.

at line 141
static cleanDir(string $target, bool $rmdir = TRUE, bool $verbose = TRUE)

Delete a directory given a path name, delete children directories and files if needed

Parameters

string $target The path name.
bool $rmdir
bool $verbose

Exceptions

Exception

at line 187
static string concat(array $files, string $delim = '')

Concatenate several files.

Parameters

array $files List of file names.
string $delim An optional delimiter to put between files.

Return Value

string

at line 204
static copyDir(string $source, string $destination)

Parameters

string $source
string $destination

at line 230
static bool toUtf8(string $name)

Given a file name, recode it (in place!) to UTF-8

Parameters

string $name Name of file.

Return Value

bool whether the file was recoded properly

at line 276
static string addTrailingSlash(string $path, string $slash = NULL)

Appends a slash to the end of a string if it doesn't already end with one

Parameters

string $path
string $slash

Return Value

string

at line 301
static string createFakeFile(string $dir, string $contents = 'delete me', string $fileName = NULL)

Save a fake file somewhere

Parameters

string $dir The directory where the file should be saved.
string $contents Optional: the contents of the file.
string $fileName

Return Value

string The filename saved, or FALSE on failure.

at line 319
static sourceSQLFile(string|NULL $dsn, string $fileName, string $prefix = NULL, bool $dieOnErrors = TRUE)

Parameters

string|NULL $dsn Use NULL to load the default/active connection from CRM_Core_DAO. Otherwise, give a full DSN string.
string $fileName
string $prefix
bool $dieOnErrors

at line 336
static runSqlQuery(string|NULL $dsn, string $queryString, string $prefix = NULL, bool $dieOnErrors = TRUE)

Parameters

string|NULL $dsn
string $queryString
string $prefix
bool $dieOnErrors

at line 388
static string stripComments(string $string)

Strips comment from a possibly multiline SQL string

Parameters

string $string

Return Value

string stripped string

at line 397
static bool isExtensionSafe($ext)

Parameters

$ext

Return Value

bool

at line 430
static bool isIncludable(string $name)

Determine whether a given file is listed in the PHP include path.

Parameters

string $name Name of file.

Return Value

bool whether the file can be include()d or require()d

at line 448
static mixed cleanFileName($name)

Remove the 32 bit md5 we add to the fileName also remove the unknown tag if we added it.

Parameters

$name

Return Value

mixed

at line 461
static string makeFileName(string $name)

Make a valid file name.

Parameters

string $name

Return Value

string

at line 484
static mixed duplicate($filePath)

Copies a file

Parameters

$filePath

Return Value

mixed

at line 501
static array getFilesByExtension(string $path, string $ext)

Get files for the extension.

Parameters

string $path
string $ext

Return Value

array

at line 522
static restrictAccess(string $dir, bool $overwrite = FALSE)

Restrict access to a given directory (by planting there a restrictive .htaccess file)

Parameters

string $dir The directory to be secured.
bool $overwrite

at line 556
static restrictBrowsing($publicDir)

Restrict remote users from browsing the given directory.

Parameters

$publicDir

at line 594
static baseFilePath() deprecated

deprecated

(Deprecated) Create the file-path from which all other internal paths are computed. This implementation determines it as dirname(CIVICRM_TEMPLATE_COMPILEDIR).

This approach is problematic - e.g. it prevents one from authentically splitting the CIVICRM_TEMPLATE_COMPILEDIR away from other dirs. The implementation is preserved for backwards compatibility (and should only be called by CMS-adapters and by Civi\Core\Paths).

Do not use it for new path construction logic. Instead, use Civi::paths().

See also

Civi::paths
Paths

at line 627
static bool isAbsolute(string $path)

Determine if a path is absolute.

Parameters

string $path

Return Value

bool TRUE if absolute. FALSE if relative.

at line 648
static string relativeDirectory($directory) deprecated

deprecated Computation of a relative path requires some base. This implementation is problematic because it relies on an implicit base which was constructed problematically.

Parameters

$directory

Return Value

string

at line 678
static string absoluteDirectory($directory, string $basePath)

Parameters

$directory
string $basePath The base path when evaluating relative paths. Should include trailing slash.

Return Value

string

at line 710
static string relativize($directory, $basePath)

Make a file path relative to some base dir.

Parameters

$directory
$basePath

Return Value

string

at line 733
static string, tempnam(string $prefix = 'tmp-')

Create a path to a temporary file which can endure for multiple requests.

Parameters

string $prefix

Return Value

string, path to an openable/writable file

See also

tempnam

at line 751
static string, tempdir(string $prefix = 'tmp-')

Create a path to a temporary directory which can endure for multiple requests.

Parameters

string $prefix

Return Value

string, path to an openable/writable directory; ends with '/'

See also

tempnam

at line 771
static array(string) findFiles(string $dir, string $pattern, bool $relative = FALSE)

Search directory tree for files which match a glob pattern.

Note: Dot-directories (like "..", ".git", or ".svn") will be ignored.

Parameters

string $dir base dir.
string $pattern glob pattern, eg "*.txt".
bool $relative TRUE if paths should be made relative to $dir

Return Value

array(string)

at line 813
static bool isChildPath(string $parent, string $child, bool $checkRealPath = TRUE)

Determine if $child is a sub-directory of $parent

Parameters

string $parent
string $child
bool $checkRealPath

Return Value

bool

at line 848
static bool replaceDir(string $fromDir, string $toDir, bool $verbose = FALSE)

Move $fromDir to $toDir, replacing/deleting any pre-existing content.

Parameters

string $fromDir The directory which should be moved.
string $toDir The new location of the directory.
bool $verbose

Return Value

bool TRUE on success

at line 872
static formatFile(array $param, string $fileName, array $extraParams = [])

Format file.

Parameters

array $param
string $fileName
array $extraParams

at line 898
static string getFileURL(string $path, string $fileType, string $url = NULL)

Return formatted file URL, like for image file return image url with image icon

Parameters

string $path Absoulte file path
string $fileType
string $url File preview link e.g. https://example.com/civicrm/file?reset=1&filename=image.png&mime-type=image/png

Return Value

string $url

at line 936
static string getImageURL(string $imageURL)

Return formatted image icon

Parameters

string $imageURL Contact's image url

Return Value

string $url

at line 991
static string resizeImage(string $sourceFile, int $targetWidth, int $targetHeight, string $suffix = "", bool $preserveAspect = TRUE)

Resize an image.

Parameters

string $sourceFile Filesystem path to existing image on server
int $targetWidth New width desired, in pixels
int $targetHeight New height desired, in pixels
string $suffix = "" If supplied, the image will be renamed to include this suffix. For example if the original file name is "foo.png" and $suffix = "_bar", then the final file name will be "foo_bar.png".
bool $preserveAspect = TRUE When TRUE $width and $height will be used as a bounding box, outside of which the resized image will not extend. When FALSE, the image will be resized exactly to $width and $height, even if it means stretching it.

Return Value

string Path to image

Exceptions

CRM_Core_Exception Under the following conditions - When GD is not available. - When the source file is not an image.

at line 1064
static string getIconFromMimeType(string $mimeType)

Get file icon class for specific MIME Type

Parameters

string $mimeType

Return Value

string

at line 1083
static bool isValidFileName(string $fileName = NULL)

Is the filename a safe and valid filename passed in from URL

Parameters

string $fileName

Return Value

bool

at line 1101
static array getAcceptableExtensionsForMimeType(string $mimeType = NULL)

Get the extensions that this MimeTpe is for

Parameters

string $mimeType the mime-type we want extensions for

Return Value

array

at line 1117
static string getExtensionFromPath(string $path)

Get the extension of a file based on its path

Parameters

string $path path of the file to query

Return Value

string