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()

Create the base file path from which all our internal directories are offset. This is derived from the template compile directory set

static bool
isAbsolute(string $path)

Determine if a path is absolute.

static string
relativeDirectory($directory)

No description

static string
absoluteDirectory($directory, string|NULL $basePath = NULL)

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 = array())

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
getIconFromMimeType(string $mimeType)

Get file icon class for specific MIME Type

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 548
static restrictBrowsing($publicDir)

Restrict remote users from browsing the given directory.

Parameters

$publicDir

at line 575
static baseFilePath()

Create the base file path from which all our internal directories are offset. This is derived from the template compile directory set

at line 608
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 625
static string relativeDirectory($directory)

Parameters

$directory

Return Value

string

at line 655
static string absoluteDirectory($directory, string|NULL $basePath = NULL)

Parameters

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

Return Value

string

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

Make a file path relative to some base dir.

Parameters

$directory
$basePath

Return Value

string

at line 706
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 724
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 744
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 786
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 820
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 844
static formatFile(array $param, string $fileName, array $extraParams = array())

Format file.

Parameters

array $param
string $fileName
array $extraParams

at line 870
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 908
static string getImageURL(string $imageURL)

Return formatted image icon

Parameters

string $imageURL Contact's image url

Return Value

string $url

at line 926
static string getIconFromMimeType(string $mimeType)

Get file icon class for specific MIME Type

Parameters

string $mimeType

Return Value

string