Documentation

File

File class for handling files.

Method Description Available from version
close Closes the file,
getSize Returns file size.
open Opens file and returns file pointer.
seek Searches file pointer from start,
deleteFile Deletes file,
getFileContents Returns file content as a string.
isFileExists Defines, if file exist.
putFileContents Writes data into file.

Examples

$file = new IO\File(Application::getDocumentRoot() . "/file.txt");

File data:

$isExist = $file->isExists(); // true, when file exists

$dir = $file->getDirectory(); // File directory as object IO\Directory
$dir = $file->getDirectoryName(); // File directory

$fileName = $file->getName(); // File name
$fileExt = $file->getExtension(); // File extension
$fileSize = $file->getSize(); // File size in bytes 
$contentType = $file->getContentType(); // Content-type

$createdAt = $file->getCreationTime(); // Date when created, timestamp
$accessAt = $file->getLastAccessTime(); // Date when last accessed, timestamp
$modifiedAt = $file->getModificationTime(); // Date when modified, timestamp

$perms = $file->getPermissions(); // Access permissions for file as decimal number
$perms = substr(sprintf('%o', $file->getPermissions()), -3); // Access permission to file as octal number

Actions with files:

$content = $file->getContents(); // Get file content
$file->putContents("data"); // Writes content into file with replacement
$file->putContents("data", IO\File::APPEND); // Finish writing content to the file's end
$file->readFile(); // Print file content

$file->rename(Application::getDocumentRoot() . "/new_file.txt"); // Move/rename the file
$file->delete(); // Delete the file

Some methods have static variants:

$path = Application::getDocumentRoot() . "/another_file.txt";
IO\File::isFileExists($path); // Check file existence

IO\File::getFileContents($path); // Get file content
IO\File::putFileContents($path, "data"); // Write content into file with replacement
IO\File::putFileContents($path, "data", self::APPEND); // Finish writing content into file's end

IO\File::deleteFile($path); // Delete the file


© «Bitrix24», 2001-2024