File Utility
Handle files and their references.
- getFileReferenceByUid ( $uid)
-
Loads
\TYPO3\CMS\Core\Resource\FileReference
object from sys_file_reference table via UID.- param int $uid
-
UID of the sys_file_reference record.
Example:
FileUtility::getFileReferenceByUid(123);
Copied!returns
// TYPO3\CMS\Core\Resource\FileReference { 'propertiesOfFileReference' => /* ... */, 'name' => /* ... */, 'originalFile' => /* ... */, 'mergedProperties' => /* ... */ }
Copied!- Returns
-
TYPO3\CMS\Core\Resource\FileReference
ornull
if resource doesn't exist or file is missing.
- buildFileArrayBySysFileReferenceUid ( $uid, $configuration)
-
Shorthand for
FileUtility::buildFileArrayBySysFileReference(FileUtility::getFileReferenceByUid($uid))
, accepts the UID of an FileReference instead of using the FileReference object directly.- param int $uid
-
UID of the sys_file_reference record.
- param array $configuration
-
(optional) See buildFileArrayBySysFileReference for further details.
Example:
FileUtility::buildFileArrayBySysFileReferenceUid(123);
Copied!returns
[ 'uid' => 123 'url' => 'fileadmin/user_upload/my-image-original.jpg', 'alt' => 'some text', 'title' => 'some text', 'description' => 'some text', 'link' => NULL, ]
Copied!- Returns
-
File-information array or
null
if resource doesn't exist or file is missing.
- buildFileArrayBySysFileReference ( $fileReference, $configuration)
-
Preparation of files and images in a simple array structure. Very helpful in image preparation and cropping.
- param \TYPO3\CMS\Core\Resource\FileReference
-
$fileReference
- param array $configuration
-
(optional). The configuration is based on three settings:
1.
showDetailedInformations
(bool): For all kind of files. If active, list more informations about the file (name, extension, type, mimetype, size, basename
).Warning
This has an noticeable impact on performance when a huge amount of files is processed.
2.
tcaCropVariants
(array): Just for images. List of -, which are applied to the images.Tip
Instead of putting this together yourself, it's easier to use the Reflection Service or Reflection Processor. These automatically read the tcaCropVariants from the corresponding TCA configurations of the images.
3.
processingConfigurationForCrop
(array): Just for images. Configuration how the image should be proceed in the differenttcaCropVariants
. Configurations likemaxWidth
,minWidth
,width
(applies also forheigth
) are possible.Example:
FileUtility::buildFileArrayBySysFileReference( $aFileReference, [ // show informations about file 'showDetailedInformations' => true, // set cropping variants 'tcaCropVariants' => [ 'desktop' => [ /* .. */ 'cropArea' => [ 'x' => 0.0, 'y' => 0.0, 'width' => 1.0, 'height' => 1.0, ] ], 'mobile' => [ /* .. */ 'cropArea' => [ 'x' => 0.0, 'y' => 0.0, 'width' => 1.0, 'height' => 1.0, ] ] ], // set image rendering instructions for the different cropping variants 'processingConfigurationForCrop' => [ 'desktop' => [ 'maxWidth' => 3000 ], // will be ignored, because we have no tcaCropVariants['medium'] informations 'medium' => [ 'maxWidth' => 1920 ], // will be ignored, because we have no tcaCropVariants['tablet'] informations 'tablet' => [ 'maxWidth' => 1024 ], 'mobile' => [ 'maxWidth' => 920 ] ] ] );
Copied!returns
[ // base informations 'uid' => 1 'url' => 'fileadmin/user_upload/my-image-original.jpg', 'alt' => 'some text', 'title' => 'some text', 'description' => 'some text', 'link' => NULL, // detailed informations 'name' => 'my-image-original.jpg', 'extension' => 'jpg', 'type' => 2, 'mimetype' => 'image/jpeg', 'size' => 2313, 'basename' => 'my-image-original', // 'cropped' is just available for image-files 'cropped' => [ 'desktop' => 'fileadmin/_processed_/1/my-image-original-max-width-3000.jpg', 'mobile' => 'fileadmin/_processed_/1/my-image-original-max-width-920.jpg' ] // if the extension "focuspoint" is installed, you also get the following properties 'has_focuspoint' => true, 'focuspoint' => [ 'x' => 0.1, 'y' => 0.2, 'w' => 0.3, 'h' => 0.4 ] ]
Copied!- Returns
-
File-information array or
null
if resource doesn't exist or file is missing.
- buildFileArrayByFile ( $file, $configuration)
-
Same as
buildFileArrayBySysFileReference
based on a file object- param \TYPO3\CMS\Core\Resource\File $file
-
The file object
- param array $configuration
-
(optional) See buildFileArrayBySysFileReference for further details.
- Returns
-
File-information array or
null
if resource doesn't exist or file is missing.
- buildFileArrayByPath ( $path, $configuration)
-
Same as
buildFileArrayBySysFileReference
based on a file path.- param string $path
-
The file path
- param array $configuration
-
(optional) See buildFileArrayBySysFileReference for further details.
- Returns
-
File-information array or
null
if resource doesn't exist or file is missing.
- getFileByPath ( $path)
-
Returns the File object to a given path.
- param $path $path
-
UID of the sys_file_reference record.
- Returns
-
File object or
null
if resource doesn't exist or file is missing.
- humanFilesize ( $bytes, $decimals = 2)
-
Converts filesizes in a human readable format.
- param int $bytes
-
Size of file in bytes.
- param int $decimals
-
(optional) Length of decimals.
Example:
FileUtility::humanFilesize(123456); // 120.56 kB FileUtility::humanFilesize(123456, 0); // 121 kB
Copied!- Returns
-
Filesize in human readable format.