To manage files and directories within a domain, use the pm_FileManager class. This class lets you create and remove files and directories, check for existence of files and directories, read and write data to files, and also change file and directory permissions (only on Linux).

For security considerations, we strongly recommend that you do not use system calls such as fopen or file_get_contents in the code of your extension. When you use such calls, the extension works with files on behalf of the psaadm user. In turn, if you use the pm_FileManager class, the manipulations are performed on behalf of the domain owner’s system user.

Descriptions and usage examples for some of the methods can be found below.

scanDir() - returns a list of files and directories inside the specified path.

array scanDir(string $path [, bool$skipDots = false [, bool $showSystemFiles = true ]])  ## Parameters • path (type: string) The directory that will be scanned. • skipDots (type: boolean, default value: false) If the optional parameter skipDots is set to “true”, then the root directories “..” and “.” will be excluded from the resulting array, otherwise they will be included. • showSystemFiles (type: boolean, default value: true) If the optional parameter showSystemFiles is set to “true”, then hidden files (for example, system files) will be included in resulting array, otherwise they will be absent. ## Return Values Returns an array containing the files and directories in the directory. ## Examples Directory tree $ ls -a var
.  ..  config.json  .gitkeep


Example

$result = (new \pm_FileManager())->scanDir(\pm_Context::getVarDir()); // Default use // analogue: //$result = (new \pm_FileManager())->scanDir(\pm_Context::getVarDir(), false, true);
var_dump($result);  Output array(4) { [0]=> string(1) "." [1]=> string(2) ".." [2]=> string(11) "config.json" [3]=> string(8) ".gitkeep" }  Example $result = (new \pm_FileManager())->scanDir(\pm_Context::getVarDir(), true);
// analogue:
// $result = (new \pm_FileManager())->scanDir(\pm_Context::getVarDir(), true, true); var_dump($result);


Output

array(2) {
[0]=>
string(11) "config.json"
[1]=>
string(8) ".gitkeep"
}


Example

$result = (new \pm_FileManager())->scanDir(\pm_Context::getVarDir(), false, false); var_dump($result);


Output

array(3) {
[0]=>
string(1) "."
[1]=>
string(2) ".."
[2]=>
string(11) "config.json"
}


Example

$result = (new \pm_FileManager())->scanDir(\pm_Context::getVarDir(), true, false); var_dump($result);


Output

array(1) {
[0]=>
string(11) "config.json"
}


joinPath() - returns joined path parts.



## Parameters

• path (type: string)

## Return Values

“True” if the path is inside the base directory, “false” otherwise.

## Examples

Example

$result = (new \pm_FileManager())->isSubpath('/var/www/vhosts/a.a/httpdocs/qwe/qwe/ddd/wqecfds'); var_dump($result);


Output

bool(true)


Example

// assuming that the webspace document root is '/var/www/vhosts/a.a/'
$result1 = (new \pm_FileManager(1))->isSubpath('/var/www/vhosts/a.a/httpdocs/qwe/qwe/ddd/wqecfds');$result2 = (new \pm_FileManager(1))->isSubpath('/var/www/vhosts/b.b/httpdocs/qwe/qwe/ddd/wqecfds');
var_dump($result1); var_dump($result2);


Output

bool(true)
bool(false)


getRelativeFilePath() - returns a relative path inside the webspace root directory for the specified absolute path.

string getRelativeFilePath(string $path)  ## Parameters • path (type: string, an absolute path) ## Return Values A relative path inside the base directory. ## Examples Example $result = (new \pm_FileManager())->getRelativeFilePath('/var/www/vhosts/a.a/httpdocs/qwe/qwe/ddd/wqecfds');
var_dump($result);  Output string(47) "var/www/vhosts/a.a/httpdocs/qwe/qwe/ddd/wqecfds"  Example // assuming that webspace document root is '/var/www/vhosts/a.a/'$result1 = (new \pm_FileManager(1))->getRelativeFilePath('/var/www/vhosts/a.a/httpdocs/qwe/qwe/ddd/wqecfds');
$result2 = (new \pm_FileManager(1))->getRelativeFilePath('/var/www/vhosts/b.b/httpdocs/qwe/qwe/ddd/wqecfds'); var_dump($result1);
var_dump($result2);  Output string(28) "httpdocs/qwe/qwe/ddd/wqecfds" string(35) "../b.b/httpdocs/qwe/qwe/ddd/wqecfds"  copyFile() - copies file system objects (files and directories) from the source to the destination. Directories are copied recursively. ## Usage copyFile(string$source, string $destination)  ## Parameters • source (type: string, the full path to the source) • destination (type: string, the full path to the destination) ## Example (new \pm_FileManager())->copyFile('/tmp/source', '/tmp/destination');  Below is an example of working with domain files. In this example, some manipulations are applied to a file content. The following methods are used: Use the following code: $fileManager = new pm_FileManager($domainId);$filePath = $fileManager->getFilePath("$currentDir/$file"); if ($fileManager->fileExists($filePath)) {$fileData = $fileManager->fileGetContents($filePath);
// Some manipulation with file data...
$fileData = str_replace('foo', 'bar',$fileData);

$fileManager->filePutContents($filePath, $fileData); }  The $filePath path here is an absolute path. It is formed from the relative path within the domain directory \$currentDir by the call of the getFilePath() method.