Documentation

Disk

\Bitrix\Disk - Disk module namespace.

Before using Disk module check if it is installed and connect it via the following structure:

\Bitrix\Main\Loader::includeModule('disk');

Examples

Getting storage

<?php 
if (\Bitrix\Main\Loader::includeModule('disk')) 
{ 
    $driver = \Bitrix\Disk\Driver::getInstance(); 
    $storage = $driver->getStorageByUserId(1);//user 
    $storage = $driver->getStorageByGroupId(33);//groups 
    $storage = $driver->getStorageByCommonId('shared_files_s1');//identifier 
    $storage = \Bitrix\Disk\Storage::loadById(66);//with load ID 
    if ($storage) 
    { 
        //now you can work with storage 
    } 
} 

Creating folder in storage

<?php 
if (\Bitrix\Main\Loader::includeModule('disk')) 
{ 
    $storage = \Bitrix\Disk\Driver::getInstance()->getStorageByUserId(1); 
    if ($storage) 
    { 
        $folder = $storage->addFolder( 
            array( 
                'NAME' => 'New folder', 
                'CREATED_BY' => 1 
            ) 
        ); 
    } 
}

Subfolder search and handling. (Available from Disk module version 17.0.2)

<?php 
if (\Bitrix\Main\Loader::includeModule('disk')) 
{ 
    $storage = \Bitrix\Disk\Driver::getInstance()->getStorageByUserId(1); 
    if ($storage) 
    { 
        /* 
        get the folder 
        */ 
        $folder = $folder->getChild( 
            array( 
                '=NAME' => 'New folder',  
                'TYPE' => \Bitrix\Disk\Internals\FolderTable::TYPE_FOLDER 
            ) 
        ); 
    } 
}

Searching root folder. (Available from Disk module version 17.0.2)

<?php 
if (\Bitrix\Main\Loader::includeModule('disk')) 
{ 
    $storage = \Bitrix\Disk\Driver::getInstance()->getStorageByUserId(1); 
    if ($storage) 
    { 
        $folder = $storage->getRootObject(); 
        $folder = $folder->getChild( 
            array( 
                '=NAME' => 'New folder',  
                'TYPE' => \Bitrix\Disk\Internals\FolderTable::TYPE_FOLDER 
            ) 
        ); 
    } 
} 

Creating subfolder in a folder

<?php 
if (\Bitrix\Main\Loader::includeModule('disk'))  { 
    $storage = \Bitrix\Disk\Driver::getInstance()->getStorageByUserId(1); 
    if ($storage)  { 
        $folder = $storage->getRootObject();  
        $folder = $folder->getChild( 
            array( 
                '=NAME' => 'New folder',  
                'TYPE' => \Bitrix\Disk\Internals\FolderTable::TYPE_FOLDER 
            ) 
        ); 
        if ($folder) { 
            $folder->addSubFolder(    array( 
                'NAME' => 'New folder 2', 
                'CREATED_BY' => 1 
            )); 
        } 
    } 
}

Configuring folder access permissions

<?php 
        if ($folder) 
        { 
            $rightsManager = \Bitrix\Disk\Driver::getInstance()->getRightsManager(); 
            $accessTaskId = $rightsManager->getTaskIdByName($rightsManager::TASK_READ);//read 
            //$accessTaskId = $rightsManager->getTaskIdByName($rightsManager::TASK_EDIT);//update 
            //$accessTaskId = $rightsManager->getTaskIdByName($rightsManager::TASK_ADD);//add 
            //$accessTaskId = $rightsManager->getTaskIdByName($rightsManager::TASK_FULL);//full access permissions 
             
            $newFolder = $folder->addSubFolder( 
                array( 'NAME' => 'New folder',  'CREATED_BY' => 1), 
                array( 
                    array( 
                        'ACCESS_CODE' => 'U10', // for user with ID=10 selected access permissions are issued
                        'TASK_ID' => $accessTaskId, 
                    ), 
                ) 
            ); 
        } 

Disabling folder access permissions. (Available from Disk module version 17.0.2.)

<?php 
        if ($folder) 
        { 
            $rightsManager = \Bitrix\Disk\Driver::getInstance()->getRightsManager(); 
            $accessTaskId = $rightsManager->getTaskIdByName($rightsManager::TASK_READ);//read 
            //$accessTaskId = $rightsManager->getTaskIdByName($rightsManager::TASK_EDIT);//update 
            //$accessTaskId = $rightsManager->getTaskIdByName($rightsManager::TASK_ADD);//add 
            //$accessTaskId = $rightsManager->getTaskIdByName($rightsManager::TASK_FULL);//full access permissions 
             
            $newFolder = $folder->addSubFolder( 
                array( 'NAME' => 'New folder',  'CREATED_BY' => 1), 
                array( 
                    array( 
                        ‘NEGATIVE' => true, 
                        'ACCESS_CODE' => 'U10', 
                        'TASK_ID' => $accessTaskId, 
                    ), 
                ) 
            ); 
        }

Upload file to folder.

<?php 
if (\Bitrix\Main\Loader::includeModule('disk')) 
{ 
    $storage = \Bitrix\Disk\Driver::getInstance()->getStorageByUserId(1); 
    if ($storage) 
    { 
        $folder = $folder->getChild( 
            array( 
                '=NAME' => 'New folder',  
                'TYPE' => \Bitrix\Disk\Internals\FolderTable::TYPE_FOLDER 
            ) 
        ); 
        if ($folder) 
        { 
            $fileArray = \CFile::MakeFileArray($_SERVER['DOCUMENT_ROOT'].'/test.jpg'); 
            $file = $folder->uploadFile($fileArray, array( 
                'CREATED_BY' => 1 
            )); 
        } 
    } 
}

Upload file to root folder.

if (\Bitrix\Main\Loader::includeModule('disk')) 
{ 
    $storage = \Bitrix\Disk\Driver::getInstance()->getStorageByUserId(1); 
    if ($storage) 
    { 
        $folder = $storage->getRootObject(); 
        $fileArray = \CFile::MakeFileArray($_SERVER['DOCUMENT_ROOT'].'/test.jpg');  
        $file = $folder->uploadFile($fileArray, array(  
            'CREATED_BY' => 1  
        ));  
    } 
}

File object high-level handling.

<?php 
$newFile->markDeleted($deletedBy); 
$newFile->delete($deletedBy);

File search. File can be handled only by entering the folder.

<?php 
if (\Bitrix\Main\Loader::includeModule('disk')) 
{ 
    $storage = \Bitrix\Disk\Driver::getInstance()->getStorageByUserId(1); 
    if ($storage) 
    { 
        $folder = $storage->getRootObject(); 
        $file = $file->getChild( 
            array( 
                '=NAME' => 'test.jpg',  
                'TYPE' => \Bitrix\Disk\Internals\FileTable::TYPE_FILE 
            ) 
        ); 
        if ($file) 
        { 
            // 
        } 
    } 
} 

Getting file link at the portal.

<?php 
if ($file) 
{ 
    $urlManager = \Bitrix\Disk\Driver::getInstance()->getUrlManager(); 
    echo $urlManager->getPathFileDetail($file); 
}

Getting public link

<?php 
if ($file) 
{ 
    $urlManager = \Bitrix\Disk\Driver::getInstance()->getUrlManager(); 
    $extLink = $file->addExternalLink( 
        array( 
            'CREATED_BY' => 1, 
            'TYPE' => \Bitrix\Disk\Internals\ExternalLinkTable::TYPE_MANUAL, 
        ) 
    ); 
    $extLinkUrl = $urlManager->getShortUrlExternalLink( 
        array( 
            'hash' => $extLink->getHash(), 
            'action' => 'default', 
        ), 
        true 
    ); 
    echo $extLink->getHash().'<br>'; 
    echo $extLinkUrl; 
}

Getting physical file

<?php 
if ($file) 
{ 
    //getting physical file ID 
    echo $file->getFileId(); 
    //handle the file 
    CFile::ViewByUser($file->getFileId(), array('force_download' => true)); 
}

File handling

<?php 
//uploading new file version 
if ($file) 
{ 
    $fileArray = \CFile::MakeFileArray($_SERVER['DOCUMENT_ROOT'].'/test.jpg'); 
    $newVersion = $file->uploadVersion($fileArray, 1);//file, user ID 
// when less than 300 seconds passes after uploading two file versions with the same name from a single user - indicates merging in progress.
} 

//moving file to another folder 
if ($file) 
{ 
    $file->moveTo($folder, $movedBy);//folder object, user 
}

Getting physical file (for uploading, for example)

<?php 
if ($file) 
{ 
    //getting physical file ID 
    echo $file->getFileId(); 
    //handle it 
    CFile::ViewByUser($file->getFileId(), array('force_download' => true)); 
}

Verifying object and error printing

<?php 
if ($folder) 
{ 
    $folder->rename('Reports.backup'); 
} 
else 
{ 
    var_dump($folder->getErrors()); 
}

Handling module is available only via high-level methods.

<?php 
//not allowed 
FolderTable::update(); 

//use instead 
$folder->rename(‘New folder 2’);


© «Bitrix24», 2001-2022
Up