Server Files and Directories
To manage files and directories anywhere on the server, use the pm_ServerFileManager class. This class enables you to perform the same operations as the pm_FileManager class, but is applied in relation to server files. In particular, it 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).
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.
Usage
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_ServerFileManager())->scanDir(\pm_Context::getVarDir()); // Default use
// analogue:
// $result = (new \pm_ServerFileManager())->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_ServerFileManager())->scanDir(\pm_Context::getVarDir(), true);
// analogue:
// $result = (new \pm_ServerFileManager())->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_ServerFileManager())->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_ServerFileManager())->scanDir(\pm_Context::getVarDir(), true, false);
var_dump($result);
Output
array(1) {
[0]=>
string(11) "config.json"
}
joinPath() - returns joined path parts.
Usage
string joinPath(string ...$pathParts)
Parameters
- pathParts (type: string, one or more variables can be specified)
Return Values
A string containing a string representation of all specified path parts arranged in the same order, with a DIRECTORY_SEPARATOR between each path part.
Example
$result = (new \pm_ServerFileManager())->joinPath('var', 'config', 'config.json');
var_dump($result);
Output
string(22) "var/config/config.json"
isSubpath() - checks if a path is a subpath of the webspace root directory.
Usage
bool isSubpath(string $path)
Parameters
- path (type: string)
Return Values
“True” if the path is inside the base directory, “false” otherwise.
Examples
Example
$result = (new \pm_ServerFileManager())->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_ServerFileManager(1))->isSubpath('/var/www/vhosts/a.a/httpdocs/qwe/qwe/ddd/wqecfds');
$result2 = (new \pm_ServerFileManager(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.
Usage
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_ServerFileManager())->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_ServerFileManager(1))->getRelativeFilePath('/var/www/vhosts/a.a/httpdocs/qwe/qwe/ddd/wqecfds');
$result2 = (new \pm_ServerFileManager(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_ServerFileManager())->copyFile('/tmp/source', '/tmp/destination');
Below is an example of working with server files. In this example, some manipulations are applied to a file content. The following methods are used:
Use the following code:
$fileManager = new pm_ServerFileManager();
$configPath = PRODUCT_ROOT . '/admin/conf/panel.ini';
if ($fileManager->fileExists($configPath)) {
$fileData = $fileManager->fileGetContents($filePath);
// Some manipulation with file data...
$fileData = str_replace('foo', 'bar', $fileData);
$fileManager->filePutContents($filePath, $fileData);
}