Contao Utils Bundle

FileUtil
in package

Table of Contents

Constants

FILE_UTIL_CONVERT_FILE_TYPE  = 'png'

Properties

$framework  : ContaoFramework
$container  : ContainerInterface

Methods

__construct()  : mixed
addUniqueIdToFilename()  : string
Add a unique identifier to a file name.
formatSizeUnits()  : mixed
getExtensionByMimeType()  : mixed
getExtensionFromFileContent()  : mixed
getFileContentFromUuid()  : mixed
getFileExtension()  : mixed
getFileFromUuid()  : File|null
getFileIdFromPath()  : mixed
getFileLineCount()  : int|string
getFileList()  : array<string|int, mixed>
Returns the file list for a given directory.
getFolderContent()  : mixed
getFolderFromDca()  : mixed|null
Get real folder from datacontainer attribute.
getFolderFromUuid()  : bool|Folder
getParentFoldersByUuid()  : mixed
getPathFromUuid()  : string|null
getPathWithoutFilename()  : mixed
getPreviewFromPdf()  : FilesModel
convert pdf to png and return a preview file delete the other png files.
getUniqueFileNameWithinTarget()  : string|false
Get a unique filename within given target folder, remove uniqid() suffix from file (optional, add $prefix) and append file count by name to file if file with same name already exists in target folder.
retrieveFileContent()  : bool|mixed
Tries to get the binary content from a file in various sources and returns it if possible.
sanitizeFileName()  : string
Sanitize filename and removes "id-" prefix generated by contao standardize method.
sendTextAsFileToBrowser()  : mixed

Constants

FILE_UTIL_CONVERT_FILE_TYPE

public mixed FILE_UTIL_CONVERT_FILE_TYPE = 'png'

Properties

$framework

protected ContaoFramework $framework

$container

private ContainerInterface $container

Methods

__construct()

public __construct(ContainerInterface $container) : mixed
Parameters
$container : ContainerInterface

addUniqueIdToFilename()

Add a unique identifier to a file name.

public addUniqueIdToFilename(string $fileName[, string $prefix = null ][, bool $moreEntropy = true ]) : string
Parameters
$fileName : string

The file name, can be with or without path

$prefix : string = null

add a prefix to the unique identifier, with an empty prefix, the returned string will be 13 characters long

$moreEntropy : bool = true

if set to TRUE, will add additional entropy (using the combined linear congruential generator) at the end of the return value, which increases the likelihood that the result will be unique

Return values
string

Filename with timestamp based unique identifier

formatSizeUnits()

public formatSizeUnits(int $bytes[, mixed $keepTogether = false ]) : mixed
Parameters
$bytes : int
$keepTogether : mixed = false

getExtensionByMimeType()

public getExtensionByMimeType(mixed $mimeType) : mixed
Parameters
$mimeType : mixed

getExtensionFromFileContent()

public getExtensionFromFileContent(mixed $content) : mixed
Parameters
$content : mixed

getFileContentFromUuid()

public getFileContentFromUuid(mixed $uuid) : mixed
Parameters
$uuid : mixed

getFileExtension()

public getFileExtension(mixed $path) : mixed
Parameters
$path : mixed

getFileFromUuid()

public getFileFromUuid(mixed $uuid) : File|null
Parameters
$uuid : mixed
Tags
throws
Exception
Return values
File|null

Return the file object

getFileIdFromPath()

public getFileIdFromPath(mixed $path) : mixed
Parameters
$path : mixed

getFileLineCount()

public getFileLineCount(mixed $file) : int|string
Parameters
$file : mixed
Return values
int|string

getFileList()

Returns the file list for a given directory.

public getFileList(string $dir, string $baseUrl[, string $protectedBaseUrl = null ]) : array<string|int, mixed>
Parameters
$dir : string
  • the absolute local path to the directory (e.g. /dir/mydir)
$baseUrl : string
  • the relative uri (e.g. /tl_files/mydir)
$protectedBaseUrl : string = null
  • domain + request uri -> absUrl will be domain + request uri + ?file=$baseUrl/filename.ext
Return values
array<string|int, mixed>

file list containing file objects

getFolderContent()

public getFolderContent(mixed $parentIds, mixed $table[, mixed $options = [] ][, mixed $return = [] ]) : mixed
Parameters
$parentIds : mixed
$table : mixed
$options : mixed = []
$return : mixed = []

getFolderFromDca()

Get real folder from datacontainer attribute.

public getFolderFromDca(mixed $folder[, DataContainer|null $dc = null ]) : mixed|null
Parameters
$folder : mixed

The folder as uuid, function, callback array('CLASS', 'method') or string (files/...)

$dc : DataContainer|null = null

Optional \DataContainer, required for function and callback

Tags
throws
Exception

If ../ is part of the path

Return values
mixed|null

The folder path or null

getFolderFromUuid()

public getFolderFromUuid(mixed $uuid) : bool|Folder
Parameters
$uuid : mixed
Tags
throws
Exception
Return values
bool|Folder

Return the folder object

getParentFoldersByUuid()

public getParentFoldersByUuid(mixed $uuid[, array<string|int, mixed> $config = [] ]) : mixed
Parameters
$uuid : mixed
$config : array<string|int, mixed> = []

getPathFromUuid()

public getPathFromUuid(mixed $uuid[, bool $checkIfExists = true ]) : string|null

Use Utils::file instead

Parameters
$uuid : mixed
$checkIfExists : bool = true
Tags
codeCoverageIgnore
Return values
string|null

Return the path of the file, or null if not exists

getPathWithoutFilename()

public getPathWithoutFilename(mixed $pathToFile) : mixed
Parameters
$pathToFile : mixed

getPreviewFromPdf()

convert pdf to png and return a preview file delete the other png files.

public getPreviewFromPdf(FilesModel $file[, int $page = 0 ]) : FilesModel

Dublicate to PdfPreview util

Parameters
$file : FilesModel
$page : int = 0
Return values
FilesModel

getUniqueFileNameWithinTarget()

Get a unique filename within given target folder, remove uniqid() suffix from file (optional, add $prefix) and append file count by name to file if file with same name already exists in target folder.

public getUniqueFileNameWithinTarget(string $target[, string $prefix = null ][, mixed $i = 0 ]) : string|false
Parameters
$target : string

The target file path

$prefix : string = null

A uniqid prefix from the given target file, that was added to the file before and should be removed again

$i : mixed = 0

integer Internal counter for recursion usage or if you want to add the number to the file

Tags
throws
Exception
Return values
string|false

The filename with the target folder and unique id or false if something went wrong (e.g. target does not exist)

retrieveFileContent()

Tries to get the binary content from a file in various sources and returns it if possible.

public retrieveFileContent(mixed $source[, mixed $silent = true ]) : bool|mixed

Possible sources:

  • url
  • contao uuid
  • string is already a binary file content
Parameters
$source : mixed
$silent : mixed = true
Return values
bool|mixed

Returns false if the file content could not be retrieved

sanitizeFileName()

Sanitize filename and removes "id-" prefix generated by contao standardize method.

public sanitizeFileName(string $fileName[, int $maxCount = 0 ][, bool $preserveUppercase = false ]) : string
Parameters
$fileName : string

The file name, can be with or without path

$maxCount : int = 0

Max filename length

$preserveUppercase : bool = false

Set to true if you want to lower case the file name

Return values
string

The sanitized filename

sendTextAsFileToBrowser()

public sendTextAsFileToBrowser(mixed $content, mixed $fileName) : mixed
Parameters
$content : mixed
$fileName : mixed

        
On this page

Search results