File
\nn\t3::File()
Methods relating to the file system: Reading, writing, copying, moving and cleaning up files.
Overview of Methods
\nn\t3::File()->absPath($file = NULL, $resolveSymLinks = false);
Absolute path to a file on the server.
Returns the complete path from the server root, e.g. from /var/www/....
If the path was already absolute, it is returned unchanged.
\nn\t3::File()->absPath('fileadmin/image.jpg'); // => /var/www/website/fileadmin/image.jpg
\nn\t3::File()->absPath('/var/www/website/fileadmin/image.jpg'); // => /var/www/website/fileadmin/image.jpg
\nn\t3::File()->absPath('EXT:nnhelpers'); // => /var/www/website/typo3conf/ext/nnhelpers/
In addition to the file path as a string, all conceivable objects can also be transferred:
// \TYPO3\CMS\Core\Resource\Folder
\nn\t3::File()->absPath( $folderObject ); => /var/www/website/fileadmin/image.jpg
// \TYPO3\CMS\Core\Resource\File
\nn\t3::File()->absPath( $fileObject ); => /var/www/website/fileadmin/image.jpg
// \TYPO3\CMS\Extbase\Domain\Model\FileReference
\nn\t3::File()->absPath( $fileReference ); => /var/www/website/fileadmin/image.jpg
Also exists as ViewHelper:
{nnt3:file.absPath(file:'path/to/image.jpg')}
| @return boolean
\nn\t3::File()->absUrl($file = NULL);
Generate absolute URL to a file.
Returns the complete path to the file including https://.../.
// => https://www.myweb.de/fileadmin/bild.jpg
\nn\t3::File()->absUrl( 'fileadmin/image.jpg' );
\nn\t3::File()->absUrl( 'https://www.myweb.de/fileadmin/bild.jpg' );
\nn\t3::File()->absUrl( $sysFileReference );
\nn\t3::File()->absUrl( $falFile );
@param string|\TYPO3\CMS\Core\Resource\FileReference|\TYPO3\CMS\Core\Resource\File $file
@return string
\nn\t3::File()->addPathSite($file);
Specifies path to file / folder WITH absolute path
Example:
\nn\t3::File()->addPathSite('fileadmin/test.jpg');
// ==> returns var/www/website/fileadmin/test.jpg
| @return string
\nn\t3::File()->addSuffix($filename = NULL, $newSuffix = '');
Replaces the suffix for a file name.
\nn\t3::File()->addSuffix('image', 'jpg'); // => image.jpg
\nn\t3::File()->addSuffix('image.png', 'jpg'); // => image.jpg
\nn\t3::File()->addSuffix('path/to/image.png', 'jpg'); // => path/to/image.jpg
| @return string
\nn\t3::File()->cleanFilename($filename = '');
Cleans a file name
$clean = \nn\t3::File()->cleanFilename('fileadmin/noe_so_not.jpg'); // 'fileadmin/noe_so_not.jpg'
| @return string
\nn\t3::File()->copy($src = NULL, $dest = NULL, $renameIfFileExists = true);
Copies a file.
Returns false if the file could not be copied.
Returns (new) file name if copying was successful.
$filename = \nn\t3::File()->copy('fileadmin/image.jpg', 'fileadmin/image-copy.jpg');
@param string $src Path to the source file
@param string $dest Path to the destination file
@param boolean $renameIfFileExists Rename file if a file with the same name already exists at the destination location
@return string|boolean
\nn\t3::File()->createFolder($path = NULL);
Create a folder in fileadmin/
To create a folder outside the fileadmin, use the method \nn\t3::File()->mkdir().
\nn\t3::File()->createFolder('tests');
| @return boolean
\nn\t3::File()->download($files = NULL, $filename = NULL);
Download a single file or a zipped archive.
Download as ZIP requires the PHP extension gmp. If extension is not available,
the .tar variant is used. On Mac, the function is always used due to
security warnings from the Finder, the function always uses tar
\nn\t3::File()->download( 'fileadmin/test.pdf' );
\nn\t3::File()->download( $fileReference );
\nn\t3::File()->download( $sysFile );
\nn\t3::File()->download( 'fileadmin/test.pdf', 'download.pdf' );
If an array is passed, a tar/zip download is started. By passing an associative array with file name as key and path in the archive as value the file and folder structure in the zip archive can be determined.
\nn\t3::File()->download( ['fileadmin/test-1.pdf', 'fileadmin/test-2.pdf'], 'archive.zip' );
\nn\t3::File()->download( ['fileadmin/test-1.pdf'=>'one.pdf', 'fileadmin/test-2.pdf'=>'two.pdf'], 'archive.zip' );
\nn\t3::File()->download( ['fileadmin/test-1.pdf'=>'zip-folder-1/one.pdf', 'fileadmin/test-2.pdf'=>'zip-folder-2/two.pdf'], 'archive.zip' );
@param mixed $files String or array of the files to be loaded
@param mixed $filename Optional: Overwrite file name during download
@return void
\nn\t3::File()->exists($src = NULL);
Checks whether a file exists. Returns the absolute path to the file.
\nn\t3::File()->exists('fileadmin/image.jpg');
Also exists as ViewHelper:
{nnt3:file.exists(file:'path/to/image.jpg')}
| @return string|boolean
\nn\t3::File()->extractExifData($filename = '');
Save EXIF data for file in JSON.
\nn\t3::File()->extractExifData( 'yellowstone.jpg' );
| @return array
\nn\t3::File()->getData($file = '');
Get image info + EXIF data for file. Also searches for JSON file that may have been generated after processImage()
| @return array
\nn\t3::File()->getExifData($filename = '');
Get ALL EXIF data for file.
\nn\t3::File()->getExif( 'yellowstone.jpg' );
| @return array
\nn\t3::File()->getFolder($file);
Returns the folder to a file
Example:
\nn\t3::File()->getFolder('fileadmin/test/example.txt');
// ==> returns 'fileadmin/test/'
| @return string
\nn\t3::File()->getImageData($filename = '');
Get EXIF image data for file.
\nn\t3::File()->getImageData( 'yellowstone.jpg' );
| @return array
\nn\t3::File()->getImageSize($filename = '');
get imagesize for file.
\nn\t3::File()->getImageSize( 'yellowstone.jpg' );
| @return array
\nn\t3::File()->getLocationData($filename = '');
Get EXIF GEO data for file. Address data is determined automatically if possible
\nn\t3::File()->getLocationData( 'yellowstone.jpg' );
| @return array
\nn\t3::File()->getPath($file, $storage = NULL, $absolute = true);
Returns the path of a file using a file name and the storage. Example:
\nn\t3::File()->getPath('media/image.jpg', $storage);
// ==> returns '/var/www/.../fileadmin/media/image.jpg'
\nn\t3::File()->getPath('fileadmin/media/image.jpg');
// ==> returns '/var/www/.../fileadmin/media/image.jpg'
| @return string
\nn\t3::File()->getPublicUrl($obj = NULL, $absolute = false);
Gets path to the file, relative to the Typo3 installation directory (PATH_site). Can handle all types of objects.
\nn\t3::File()->getPublicUrl( $falFile ); // \TYPO3\CMS\Core\Resource\FileReference
\nn\t3::File()->getPublicUrl( $fileReference ); // \TYPO3\CMS\Extbase\Domain\Model\FileReference
\nn\t3::File()->getPublicUrl( $folder ); // \TYPO3\CMS\Core\Resource\Folder
\nn\t3::File()->getPublicUrl( $folder, true ); // https://.../fileadmin/bild.jpg
| @return string
\nn\t3::File()->getRelativePathInStorage($file, $storage = NULL);
Returns the relative path of a file to the specified storage.
Example:
\nn\t3::File()->getRelativePathInStorage('fileadmin/media/image.jpg', $storage);
// ==> returns 'media/image.jpg'
| @return string
\nn\t3::File()->getStorage($file, $createIfNotExists = false);
Finds a matching sys_file_storage for a file or folder path. To do this, searches through all sys_file_storage entries and compares whether the basePath of the storage matches the path of the file.
\nn\t3::File()->getStorage('fileadmin/test/example.txt');
\nn\t3::File()->getStorage( $falFile );
\nn\t3::File()->getStorage( $sysFileReference );
// returns ResourceStorage with basePath "fileadmin/"
| @return ResourceStorage
\nn\t3::File()->isAllowed($filename = NULL);
Specifies whether the file type is allowed
\nn\t3::File()->isForbidden('image.jpg'); => returns 'true'
\nn\t3::File()->isForbidden('hack.php'); => returns 'false'
| @return boolean
\nn\t3::File()->isConvertableToImage($filename = NULL);
Specifies whether the file can be converted to an image
\nn\t3::File()->isConvertableToImage('image.jpg'); => returns true
\nn\t3::File()->isConvertableToImage('text.ppt'); => returns false
| @return boolean
\nn\t3::File()->isExternalVideo($url = NULL);
Indicates whether it is a video on YouTube / Vimeo. If yes, an array with embedding information is returned.
\nn\t3::File()->isExternalVideo('http://...');
| @return array|boolean
\nn\t3::File()->isFolder($file);
Returns whether the specified path is a folder
Example:
\nn\t3::File()->isFolder('fileadmin'); // => returns true
| @return boolean
\nn\t3::File()->isForbidden($filename = NULL, $allowed = []);
Indicates whether the file type is prohibited
\nn\t3::File()->isForbidden('image.jpg'); => returns 'false'
\nn\t3::File()->isForbidden('image.xyz', ['xyz']); => returns 'false'
\nn\t3::File()->isForbidden('hack.php'); => returns 'true'
\nn\t3::File()->isForbidden('.htaccess'); => returns 'true'
@param string $filename
@param array $allowed
@return boolean
\nn\t3::File()->isImage($filename = NULL);
Indicates whether the file is in an image
\nn\t3::File()->isImage('image.jpg'); => returns true
\nn\t3::File()->isImage('text.ppt'); => returns false
| @return boolean
\nn\t3::File()->isVideo($filename = NULL);
Indicates whether the file is a video
\nn\t3::File()->isVideo('path/to/video.mp4'); => returns true
| @return boolean
\nn\t3::File()->mkdir($path = '');
Create a folder
\nn\t3::File()->mkdir( 'fileadmin/my/folder/' );
\nn\t3::File()->mkdir( '1:/my/folder/' );
| @return boolean
\nn\t3::File()->move($src = NULL, $dest = NULL);
Moves a file
\nn\t3::File()->move('fileadmin/image.jpg', 'fileadmin/image-copy.jpg');
| @return boolean
\nn\t3::File()->moveUploadedFile($src = NULL, $dest = NULL);
Move an upload file to the target directory.
Can be the absolute path to the tmp file of the upload â or a TYPO3\CMS\Core\Http\UploadedFile,
which can be retrieved in the controller via $this->request->getUploadedFiles().
\nn\t3::File()->moveUploadedFile('/tmp/xjauGSaudsha', 'fileadmin/image-copy.jpg');
\nn\t3::File()->moveUploadedFile( $fileObj, 'fileadmin/image-copy.jpg');
| @return string
\nn\t3::File()->normalizePath($path);
Resolves ../../ specifications in path. Works with both existing paths (via realpath) and non-existing paths. non-existing paths.
\nn\t3::File()->normalizePath( 'fileadmin/test/../image.jpg' ); => fileadmin/image.jpg
| @return string
\nn\t3::File()->process($fileObj = '', $processing = [], $returnProcessedImage = false);
Calculates an image via maxWidth, maxHeight etc.
Simple version of \nn\t3::File()->processImage()
Can be used if only the generation of reduced images is required
without taking into account corrections to camera alignment etc.
- Since the crop settings are stored in FileReference and not File,
cropVariantonly works when aFileReferenceis passed.
\nn\t3::File()->process( 'fileadmin/imgs/portrait.jpg', ['maxWidth'=>200] );
\nn\t3::File()->process( '1:/images/portrait.jpg', ['maxWidth'=>200] );
\nn\t3::File()->process( $sysFile, ['maxWidth'=>200] );
\nn\t3::File()->process( $sysFile, ['maxWidth'=>200, 'absolute'=>true] );
\nn\t3::File()->process( $sysFileReference, ['maxWidth'=>200, 'cropVariant'=>'square'] );
With the parameter $returnProcessedImage = true, not the file path to the new image
but the processedImage object is returned.
\nn\t3::File()->process( 'fileadmin/imgs/portrait.jpg', ['maxWidth'=>200], true );
| @return string
\nn\t3::File()->processImage($filenameOrSysFile = '', $processing = []);
Can be called directly after upload_copy_move().
Corrects the orientation of the image, which may have been saved in EXIF data.
Simply use the method \nn\t3::File()->process() for the maxWidth statement.
Instructions for $processing:
| correctOrientation => Correct rotation (e.g. because photo was uploaded from smartphone)
| @return string
\nn\t3::File()->read($src = NULL);
Retrieves the content of a file
\nn\t3::File()->read('fileadmin/text.txt');
| @return string|boolean
\nn\t3::File()->relPath($path = '');
relative path (from the current script) to a file / directory. If no path is specified, the Typo3 root directory is returned.
\nn\t3::File()->relPath( $file ); => ../fileadmin/image.jpg
\nn\t3::File()->relPath(); => ../
| @return string
\nn\t3::File()->resolvePathPrefixes($file = NULL, $absolute = false);
EXT: Resolve prefix to relative path specification
\nn\t3::File()->resolvePathPrefixes('EXT:extname'); => /typo3conf/ext/extname/
\nn\t3::File()->resolvePathPrefixes('EXT:extname/'); => /typo3conf/ext/extname/
\nn\t3::File()->resolvePathPrefixes('EXT:extname/image.jpg'); => /typo3conf/ext/extname/image.jpg
\nn\t3::File()->resolvePathPrefixes('1:/uploads/image.jpg', true); => /var/www/website/fileadmin/uploads/image.jpg
| @return string
\nn\t3::File()->sendDownloadHeader($filename = '', $filesize = NULL);
Send PHP header for download.
If the file physically exists, the filesize is determined automatically.
\nn\t3::File()->sendDownloadHeader( 'download.jpg' );
\nn\t3::File()->sendDownloadHeader( 'path/to/file/download.jpg' );
\nn\t3::File()->sendDownloadHeader( 'fakedatei.jpg', 1200 );
| @return void
\nn\t3::File()->size($src = NULL);
Returns the file size of a file in bytes If file does not exist, 0 is returned.
\nn\t3::File()->size('fileadmin/image.jpg');
| @return integer
\nn\t3::File()->stripBaseUrl($file);
Removes the URL if it corresponds to the current domain
Example:
\nn\t3::File()->stripBaseUrl('https://www.my-web.de/fileadmin/test.jpg'); ==> fileadmin/test.jpg
\nn\t3::File()->stripBaseUrl('https://www.other-web.de/example.jpg'); ==> https://www.other-web.de/example.jpg
| @return string
\nn\t3::File()->stripPathSite($file, $prefix = false);
Specifies the path to the file / folder WITHOUT an absolute path. Optionally, a prefix can be specified.
Example:
\nn\t3::File()->stripPathSite('var/www/website/fileadmin/test.jpg'); ==> fileadmin/test.jpg
\nn\t3::File()->stripPathSite('var/www/website/fileadmin/test.jpg', true); ==> var/www/website/fileadmin/test.jpg
\nn\t3::File()->stripPathSite('fileadmin/test.jpg', true); ==> var/www/website/fileadmin/test.jpg
\nn\t3::File()->stripPathSite('fileadmin/test.jpg', '../../'); ==> ../../fileadmin/test.jpg
| @return string
\nn\t3::File()->suffix($filename = NULL);
Returns the suffix of the file
\nn\t3::File()->suffix('image.jpg'); => returns 'jpg'
| @return string
\nn\t3::File()->suffixForMimeType($mime = '');
Returns the suffix for a specific mime type / content type. Very reduced version - only a few types covered. Extensive version: https://bit.ly/3B9KrNA
\nn\t3::File()->suffixForMimeType('image/jpeg'); => returns 'jpg'
| @return string
\nn\t3::File()->type($filename = NULL);
Returns the type of file based on the file suffix
\nn\t3::File()->type('image.jpg'); => returns 'image'
\nn\t3::File()->type('text.doc'); => returns 'document'
| @return string
\nn\t3::File()->uniqueFilename($filename = '');
Creates a unique file name for the file if there is already a file with an identical name already exists in the target directory already exists in the target directory.
$name = \nn\t3::File()->uniqueFilename('fileadmin/01.jpg'); // 'fileadmin/01-1.jpg'
| @return string
\nn\t3::File()->unlink($file = NULL);
Deletes a file completely from the server.
Also deletes all sys_file and sys_file_references that refer to the file.
For security reasons, no PHP or HTML files can be deleted.
\nn\t3::File()->unlink('fileadmin/image.jpg'); // Path to the image
\nn\t3::File()->unlink('/abs/path/to/file/fileadmin/image.jpg'); // absolute path to the image
\nn\t3::File()->unlink('1:/my/image.jpg'); // Combined identifier notation
\nn\t3::File()->unlink( $model->getImage() ); // \TYPO3\CMS\Extbase\Domain\Model\FileReference
\nn\t3::File()->unlink( $falFile ); // \TYPO3\CMS\Core\Resource\FileReference
| @return boolean
\nn\t3::File()->write($path = NULL, $content = NULL);
Create a folder and/or file. Also creates the folders if they do not exist.
\nn\t3::File()->write('fileadmin/some/deep/folder/');
\nn\t3::File()->write('1:/some/deep/folder/');
\nn\t3::File()->write('fileadmin/some/deep/folder/file.json', 'TEXT');
| @return boolean