IO - uniqcle/Bitrix GitHub Wiki

IO - объектно-ориентированная работа с файлами, обладает тремя базовыми классами: \Path, \Directory, \File

https://dev.1c-bitrix.ru/api_d7/bitrix/main/io/index.php

File

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

Информация о файле

$isExist = $file->isExists(); // true, если файл существует

$dir = $file->getDirectory(); // Директория файла в виде объекта IO\Directory
$dir = $file->getDirectoryName(); // Директория файла

$fileName = $file->getName(); // Имя файла
$fileExt = $file->getExtension(); // Расширение файла
$fileSize = $file->getSize(); // Размер файла в байтах
$contentType = $file->getContentType(); // Content-type

$createdAt = $file->getCreationTime(); // Дата создания, timestamp
$accessAt = $file->getLastAccessTime(); // Дата последнего доступа, timestamp
$modifiedAt = $file->getModificationTime(); // Дата модификации, timestamp

$perms = $file->getPermissions(); // Права на файл в виде десятичного числа
$perms = substr(sprintf('%o', $file->getPermissions()), -3); // Права на файл в виде восьмеричного числа

Действия над файлами

$content = $file->getContents(); // Получить содержание файла
$file->putContents("data"); // Записать содержимое в файл с заменой
$file->putContents("data", IO\File::APPEND); // Дописать содержимое в конец файла
$file->readFile(); // Вывести содержимое файла

$file->rename(Application::getDocumentRoot() . "/new_file.txt"); // Переместить/переименовать файл
$file->delete(); // Удалить файл
Работа со своим классом File

uniqcle/file.class/class.php

...
use \Uniqcle\OOP\File;
...
public function executeComponent()
    {
        $this -> includeComponentLang('class.php');

        if($this -> checkModules()){

            $file = new File('file.txt');

            $this->arResult = $file->write('test test test');

            $this->includeComponentTemplate();
        }
    }

lib/file.php

namespace Uniqcle\OOP;

use  \Bitrix\Main\Application,
      Bitrix\Main\IO;

class File
{
    public $file;
    public $handle; // указатель на открытй файл

    public function __construct($file){
        $this->file = Application::getDocumentRoot() . "/files/". $file ;
    }

    public function write($text){
        $file = new IO\File( $this->file );
        $file->putContents($text, IO\File::APPEND);
    }
}
Работа с файлом
use Bitrix\Main\IO;
use Bitrix\Main\Application;

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

//file info
$isExist = $file->isExists();

$dir = $file->getDirectory();
$dir = $file->getDirectoryName();

$fileName = $file->getName();
$fileExt = $file->getExtension();
$fileSize = $file->getSize();
$contentType = $file->getContentType();

$createdAt = $file->getCreationTime();
$accessAt = $file->getLastAccessTime();
$modifiedAt = $file->getModificationTime();

$perms = $file->getPermissions();
$perms = substr(sprintf('%o', $file->getPermissions()), -3);

//file operations
$content = $file->getContents();
$file->putContents("data");
$file->putContents("data", IO\File::APPEND);
$file->readFile();

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

Bitrix\Main\IO\Directory

use Bitrix\Main\IO;
use Bitrix\Main\Application;

$dir = new IO\Directory(Application::getDocumentRoot() . "/test/");

$dir->create();

//dir info
$isExist = $dir->isExists();

$createdAt = $dir->getCreationTime();
$accessAt = $dir->getLastAccessTime();
$modifiedAt = $dir->getModificationTime();

$perms = $dir->getPermissions();
$perms = substr(sprintf('%o', $dir->getPermissions()), -3);

//dir operations
$childDir = $dir->createSubdirectory("child");
$dir->rename(Application::getDocumentRoot() . "/another_path/");
$dir->delete();

$files = $dir->getChildren();

Bitrix\Main\IO\Path

use Bitrix\Main\IO;
use Bitrix\Main\Application;

$path = Application::getDocumentRoot() . "/some_dir/some_file.ext";
$fileExt = IO\Path::getExtension($path);
$fileName = IO\Path::getName($path);
$fileDir = IO\Path::getDirectory($path);
⚠️ **GitHub.com Fallback** ⚠️