Directory
Jalno provides the packages\base\IO\Directory class to developers for managing directories. This class is created as abstract, and therefore the programmer cannot create an object directly from this class.
Currently, four other classes with the following names are derived from the Directory class, which are used depending on the file's storage location:
| Class name | Directory storage location |
|---|---|
| packages\base\IO\Directory\Local | Local directories |
| packages\base\IO\Directory\Tmp | Temporary local directories |
| packages\base\IO\Directory\Ftp | Remote directories |
| packages\base\IO\Directory\Sftp | Remote directories |
An object of the Directory class always points to the address of a directory, but that directory does not necessarily have to exist. Sometimes we intend to create a new directory, so we first create an object from one of the Directory subclasses and then proceed to create the directory.
Methods
Methods that can be called on all Directory subclasses:
| Method name | Description |
|---|---|
| copyTo(directory $dest): bool | Copies the contents of one directory into another directory |
| copyFrom(directory $source): bool | Copies contents from another directory |
| delete(): mixed | Deletes the directory from disk |
| rename(string $newName): bool | Renames |
| move(directory $dest): bool | Moves a directory to another directory |
| make(): bool | Creates the directory |
| files(bool $recursively): array | Lists the files in the directory |
| items(bool $recursively): array | Lists the items in the directory |
| directories(bool $recursively): array | Lists the directories in the directory |
| file(string $name): File | Assigns a file to the directory |
| directory(string $name): Directory | Assigns a directory to the directory |
| size(): int | Calculates the size of a directory in bits |
| getPath(): string | Gets the path of a directory |
| isEmpty(): bool | Indicates whether the directory is empty or not |
| getDirectory(): Directory | Provides access to the folder containing this directory. |
| exists(): bool | Checks whether the directory exists |
Variables
| Name | Description |
|---|---|
| basename | The name of the folder in which it is stored. |
| directory | The path of the folder in which it is stored. |
The variables of the Directory class are directly accessible in all types.
Local directories
If the directory you intend to access is on the same server on which Jalno is installed, create an object from the packages\base\IO\Directory\Local class and pass the directory's path to it as input.
In addition to the main methods of the Directory class, the following method is also defined for local directories.
| Method name | Description |
|---|---|
| getRealPath(): string | Gets the exact path of a directory |
If the entered path does not begin with a /, addressing is done relatively, starting from the project's root directory (the same folder that contains the index.php file and the packages folder). For more information about relative addressing, refer to this article.
Example 1:
use packages\base\IO\Directory;
$directory = new Directory\Local("/home/ali/public_html/");
Example 2:
use packages\base\IO\Directory;
$directory = new Directory\Local("packages/base");
Example 3:
<?php
namespace package\packagename\controllers;
use packages\base\{Controller, Response, IO\Directory, view\Error, Packages};
class Directories extends Controller {
public function Make(): Response {
$inputs = $this->checkInputs(array(
"name" => array(
"type" => "string",
),
));
$directory = new Directory\Local("packages/packagename/storage/private/" . $inputs["name"]);
/**
* Or you can use Package::getDirectory instead
* $directory = Packages::package("packagename")->getDirectory("storage/private/" . $inputs["name"]);
*/
if ($directory->exists()) {
throw new Error("already_exists");
}
$directory->make(true);
$this->response->setData($directory->getPath(), "path");
$this->response->setStatus(true);
return $this->response;
}
}
Temporary directories
If you need a directory to be created temporarily, instantiate the packages\base\IO\Directory\Tmp class. This class receives no input in its constructor and automatically creates a local directory in the folder suggested by the operating system.
Every temporary directory is also a local directory and has all the capabilities and abilities of packages\base\IO\Directory\Local.
A temporary directory is created as soon as the Tmp object is created and is ready for reading and writing, and as soon as that object is destroyed, it is also removed from the hard disk. Destroying the object is done either explicitly with the unset statement or implicitly by the php interpreter's garbage collector.
Example 1:
use packages\base\IO\Directory;
$tmp = new Directory\Tmp();
Example 2:
<?php
namespace package\packagename\controllers;
use packages\base\{Controller, Response, IO\Directory, view\Error, Packages};
class Directories extends Controller {
public function Swap(): Response {
$inputs = $this->checkInputs(array(
"src" => array(
"type" => "string",
),
"dest" => array(
"type" => "string",
),
));
$source = new Directory\Local("packages/packagename/storage/private/" . $inputs["src"]);
/**
* Or you can use Package::getDirectory instead
* $source = Packages::package("packagename")->getDirectory("storage/private/" . $inputs["src"]);
*/
if (!$source->exists()) {
throw new Error("source_isnot_exists");
}
$destination = new Directory\Local("packages/packagename/storage/private/" . $inputs["dest"]);
/**
* Or you can use Package::getDirectory instead
* $destination = Packages::package("packagename")->getDirectory("storage/private/" . $inputs["dest"]);
*/
if (!$destination->exists()) {
throw new Error("destination_isnot_exists");
}
$tmp = new Directory\Tmp();
$source->copyTo($tmp);
$destination->copyTo($source);
$destination->copyFrom($tmp); // Or $tmp->copyTo($destination);
$this->response->setStatus(true);
return $this->response;
}
}
Remote directories / FTP
To access directories that are on other servers, you can use the packages\base\IO\Directory\Ftp class.
To access the directories, we first create an object from the Ftp class, give it the path of the desired directory on the server, and then configure the server's credentials.
Example 1:
use packages\base\IO\Directory;
$ftp = new Directory\Ftp('public_html/packages/test/dir');
$ftp->hostname = "test.com";
$ftp->password = "123";
$ftp->username = "ali";
If the entered server credentials are incorrect, we receive Warning: ftp_connect(): php_network_getaddresses: getaddrinfo failed: Name or service not known.
Example 2:
<?php
namespace package\packagename\controllers;
use packages\base\{Controller, Response, IO\Directory, view\Error, Packages};
class Directories extends Controller {
public function UploadViaFTP(): Response {
$inputs = $this->checkInputs(array(
"src" => array(
"type" => "string",
),
"dest" => array(
"type" => "string",
),
"hostname" => array(
"type" => "string",
),
"username" => array(
"type" => "string",
),
"password" => array(
"type" => "string",
),
));
$source = new Directory\Local("packages/packagename/storage/private/" . $inputs["src"]);
/**
* Or you can use Package::getDirectory instead
* $source = Packages::package("packagename")->getDirectory("storage/private/" . $inputs["src"]);
*/
if (!$source->exists()) {
throw new Error("source_isnot_exists");
}
$destination = new Directory\Ftp($inputs["dest"]);
$destination->hostname = $inputs["hostname"];
$destination->username = $inputs["username"];
$destination->password = $inputs["password"];
if (!$destination->exists()) {
$destination->make(true);
}
$source->copyTo($destination);
$this->response->setStatus(true);
return $this->response;
}
}
Remote directories / SFTP
To access remote directories over the SSH2 channel, you can also use the packages\base\IO\Directory\Sftp class.
To access the directories, we first create an object from the Sftp class; we give it the path of the desired directory on the server, and then configure the server's credentials.
Example:
use packages\base\IO\Directory;
$sftp = new Directory\Sftp('public_html/packages/test/dir');
$sftp->hostname = "test.com";
$sftp->password = "123";
$sftp->username = "ali";
$sftp->port = 7624;
If the entered server credentials are incorrect, we receive exception: packages\base\ssh\ConnectionException.
Example 2:
<?php
namespace package\packagename\controllers;
use packages\base\{Controller, Response, IO\Directory, view\Error, Packages};
class Directories extends Controller {
public function UploadViaSFTP(): Response {
$inputs = $this->checkInputs(array(
"src" => array(
"type" => "string",
),
"dest" => array(
"type" => "string",
),
"hostname" => array(
"type" => "string",
),
"username" => array(
"type" => "string",
),
"password" => array(
"type" => "string",
),
"port" => array(
"type" => "number",
"min" => 1,
"max" => 65535,
"default" => 22,
"optional" => true,
),
));
$source = new Directory\Local("packages/packagename/storage/private/" . $inputs["src"]);
/**
* Or you can use Package::getDirectory instead
* $source = Packages::package("packagename")->getDirectory("storage/private/" . $inputs["src"]);
*/
if (!$source->exists()) {
throw new Error("source_isnot_exists");
}
$destination = new Directory\Sftp($inputs["dest"]);
$destination->hostname = $inputs["hostname"];
$destination->username = $inputs["username"];
$destination->password = $inputs["password"];
$destination->port = $inputs["port"];
if (!$destination->exists()) {
$destination->make(true);
}
$source->copyTo($destination);
$this->response->setStatus(true);
return $this->response;
}
}
Creating a directory
The make() method is used to create a directory.
This method receives a boolean value as its input; if the value true is passed to the method, all nested folders are also created.
If the value false is passed to the method, only the last directory in the specified path is created.
The default value of the make() method's input argument is false.
If the value false is passed to the method and its parent directory does not exist, we receive a warning.
If the directory already exists, we receive a warning.
Example 1 of erroneous code
use packages\base\IO\Directory;
$dir = new Directory\Local("packages/jalno/non-exists-directory/dir");
$dir->make(); // or -> $dir->make(false); // Warning: mkdir(): No such file or directory
Example 2 of erroneous code
use packages\base\IO\Directory;
$dir = new Directory\Local("packages/jalno/directory/exists-dir");
$dir->make(); // or -> $dir->make(false); // Warning: mkdir(): File exists
Example 1:
use packages\base\IO\Directory;
$dir = new Directory\Local("packages/jalno/non-exists-directory/dir");
if (!$dir->exists()) {
$dir->make(true);
}
In the above example, in addition to the dir directory, its parent directories are also created if they do not exist.
Example 2:
use packages\base\IO\Directory;
$dir = new Directory\Local("packages/jalno/non-exists-directory/dir");
if (!$dir->exists() and $dir->getDirectory()->exists()) {
$dir->make();
}
In the above example, only the dir directory is created, so you must first ensure that its parent directories exist.
Example 3:
use packages\base\IO\Directory;
$ftp = new Directory\Ftp('public_html/packages/test/dir');
$ftp->hostname = "test.com";
$ftp->password = "123";
$ftp->username = "ali";
if (!$ftp->exists()) {
$ftp->make(true);
}
Example 4:
use packages\base\IO\Directory;
$sftp = new Directory\Sftp('public_html/packages/test/dir');
$sftp->hostname = "test.com";
$sftp->password = "123";
$sftp->username = "ali";
$sftp->port = 7624;
if (!$sftp->exists()) {
$sftp->make(true);
}
Deleting a directory
The delete() method is used to delete a directory.
If the directory does not exist, we receive a warning.
Example of erroneous code:
use packages\base\IO\Directory;
$dir = new Directory\Local("packages/jalno/non-exists-directory");
$dir->delete(); // Warning: scandir(packages/jalno/non-exists-directory): failed to open dir: No such file or directory
Example 1:
use packages\base\IO\Directory;
$dir = new IO\directory\local("packages/jalno/directory");
if ($dir->exists()) {
$dir->delete();
}
Example 2:
use packages\base\IO\Directory;
$ftp = new Directory\Ftp('public_html/packages/test/dir');
$ftp->hostname = "test.com";
$ftp->password = "123";
$ftp->username = "ali";
if ($ftp->exists()) {
$ftp->delete();
}
Example 3:
use packages\base\IO\Directory;
$sftp = new Directory\Sftp('public_html/packages/test/dir');
$sftp->hostname = "test.com";
$sftp->password = "123";
$sftp->username = "ali";
$sftp->port = 7624;
if ($sftp->exists()) {
$sftp->delete();
}
Moving a directory
The move() method is used to move a directory. This method receives, as its input argument, an object from the classes derived from the packages\base\IO\Directory class, and its return value is a boolean.
If the source directory does not exist, we receive a warning.
We must note that before calling the move() method, we should make sure that the destination directory does not exist.
Example of erroneous code:
use packages\base\IO\Directory;
$dir = new Directory\Local("packages/jalno/non-exists-directory");
$destination = new Directory\Local("packages/jalno/directory");
$dir->move($destination); // Warning: rename(packages/jalno/non-exists-directory,packages/jalno/directory/non-exists-directory): No such file or directory
Example 1 of correct code:
use packages\base\IO\Directory;
$dir = new Directory\Local("packages/jalno/directory");
$destination = new Directory\Local("packages/jalno/newDirectory");
if ($dir->exists() and !$destination->exists()) {
$dir->move($destination);
}
Example 2 of correct code:
use packages\base\IO\Directory;
$dir = new Directory\Local("packages/jalno/directory");
$destination = new Directory\Tmp();
if ($dir->exists()) {
$dir->move($destination);
}
Example 3 of correct code:
use packages\base\IO\Directory;
$ftp = new Directory\Ftp('public_html/packages/test/dir');
$ftp->hostname = "test.com";
$ftp->password = "123";
$ftp->username = "ali";
$destination = new Directory\Ftp('public_html/packages/test/newDir');
$destination->hostname = "test.com";
$destination->password = "123";
$destination->username = "ali";
if ($ftp->exists() and !$destination->exists()) {
$ftp->move($destination);
}
Example 4 of correct code:
use packages\base\IO\Directory;
$sftp = new Directory\Sftp('public_html/packages/test/dir');
$sftp->hostname = "test.com";
$sftp->password = "123";
$sftp->username = "ali";
$sftp->port = 7624;
$local = new Directory\Local("packages/jalno/dir");
if ($sftp->exists() and !$local->exists()) {
$sftp->move($local);
}
Calculating directory size
The size() method is used to calculate the size of a directory. The calculated size is in bytes.
If the directory does not exist, we receive a warning.
Example of erroneous code:
use packages\base\IO\Directory;
$dir = new Directory\Local("packages/jalno/non-exists-directory");
echo $dir->size(); // Warning: scandir(packages/jalno/non-exists-directory): failed to open dir: No such file or directory
Example of correct code:
use packages\base\IO\Directory;
$dir = new Directory\Local("packages/jalno/directory");
if ($dir->exists()) {
echo $dir->size();
}
Copying a directory
The copyTo() method is used to copy the contents of one directory into another directory.
The input argument of this method is an object from the classes derived from the packages\base\IO\Directory class.
If the destination directory does not exist, it is created.
If the source directory does not exist, we receive a warning.
Important note: Programmers should note that if a file (or directory) with a name similar to one in the destination directory exists in the source directory, the file (or directory) in the destination directory is replaced.
Example of erroneous code
use packages\base\IO\Directory;
$source = new Directory\Local("packages/jalno/non-exists-directory");
$destination = new Directory\Local("packages/jalno/new");
$source->copyTo($destination); // Warning: scandir(packages/jalno/non-exists-directory): failed to open dir: No such file or directory
Example 1 of correct code
use packages\base\IO\Directory;
$source = new Directory\Local("packages/jalno/directory/dir");
$destination = new Directory\Local("packages/jalno/new/dir");
if ($source->exists()) {
$source->copyTo($destination);
}
Example 2 of correct code
use packages\base\IO\Directory;
$source = new Directory\Local("packages/jalno/directory/dir");
$tmp = new Directory\Tmp();
if ($source->exists()) {
$source->copyTo($tmp);
}
Example 3 of correct code:
use packages\base\IO\Directory;
$ftp = new Directory\Ftp('public_html/packages/test/dir');
$ftp->hostname = "test.com";
$ftp->password = "123";
$ftp->username = "ali";
$destination = new Directory\Ftp('public_html/packages/test/newDir');
$destination->hostname = "test.com";
$destination->password = "123";
$destination->username = "ali";
if ($ftp->exists()) {
$ftp->copyTo($destination);
}
Example 4 of correct code:
use packages\base\IO\Directory;
$ftp = new Directory\Ftp('public_html/packages/test/dir');
$ftp->hostname = "test.com";
$ftp->password = "123";
$ftp->username = "ali";
$local = new Directory\Local('packages/jalno/dir');
if ($local->exists()) {
$local->copyTo($ftp);
}
Example 5 of correct code:
use packages\base\IO\Directory;
$sftp = new Directory\Sftp('public_html/packages/test/dir');
$sftp->hostname = "test.com";
$sftp->password = "123";
$sftp->username = "ali";
$sftp->port = 7624;
$tmp = new Directory\Tmp();
if ($sftp->exists()) {
$sftp->copyTo($tmp);
}
The copyFrom() method is used to copy contents from another directory.
We must make sure that the directory from which we intend to copy the contents exists.
Example of correct code
use packages\base\IO\Directory;
$destination = new Directory\Local("packages/jalno/directory/dir");
$source = new Directory\Local("packages/jalno/copy-items");
if ($source->exists()) {
$destination->copyFrom($source);
}
Example of incorrect code
use packages\base\IO\Directory;
$destination = new Directory\Local("packages/jalno/directory/dir");
$source = new Directory\Local("packages/jalno/non-exists-directory");
$destination->copyFrom($source); // Warning: scandir(packages/jalno/non-exists-directory): failed to open dir: No such file or directory
Renaming a directory
The rename() method is used to rename directories.
The input argument of this method is of type string; the new name is passed to the method.
If the directory does not exist, we receive a warning.
If a directory with a name similar to the new name exists in the parent directory and is not empty, we receive a warning.
Example 1 of incorrect code
use packages\base\IO\Directory;
$directory = new Directory\Local("packages/jalno/non-exists-directory");
$directory->rename("newName"); // Warning: rename(packages/jalno/non-exists-directory,packages/jalno/newName): No such file or directory
Example 2 of incorrect code
use packages\base\IO\Directory;
$directory = new Directory\Local("packages/jalno/directory");
$directory->rename("newName"); // Warning: rename(packages/jalno/directory,packages/jalno/newName): Directory not empty
Example 1 of correct code
use packages\base\IO\Directory;
$directory = new Directory\Local("packages/jalno/directory");
$checkExistsDir = $directory->getDirectory()->directory("newName")->exists();
if ($directory->exists() and !$checkExistsDir) {
$directory->rename("newName");
}
Example 2 of correct code
use packages\base\IO\Directory;
$directory = new Directory\Ftp('public_html/packages/test/dir');
$directory->hostname = "test.com";
$directory->password = "123";
$directory->username = "ali";
$checkExistsDir = $directory->getDirectory()->directory("newName")->exists();
if ($directory->exists() and !$checkExistsDir) {
$directory->rename("newName");
}
Example 3 of correct code
use packages\base\IO\Directory;
$directory = new Directory\Sftp('public_html/packages/test/dir');
$directory->hostname = "test.com";
$directory->password = "123";
$directory->username = "ali";
$directory->port = 7624;
$checkExistsDir = $directory->getDirectory()->directory("newName")->exists();
if ($directory->exists() and !$checkExistsDir) {
$directory->rename("newName");
}
Listing the directories in a directory
Using the directories() method, we get the list of folders in a directory. The method's input argument is boolean.
If the value true is passed to the method, it also returns the nested directories, and
if the value false is passed to the method, it only returns the top-level folders. The output of this method is an array in which each element is an object of the Directory class.
The input argument is true by default.
If the directory does not exist, we receive a warning.
Example of erroneous code
use packages\base\IO\Directory;
$dir = new Directory\Local("packages/jalno/non-exists-directory");
$directories = $dir->directories(); // Warning: scandir(packages/jalno/non-exists-directory): failed to open dir: No such file or directory
Example 1 of correct code:
use packages\base\IO\Directory;
$dir = new Directory\Local("packages/jalno/directory");
if ($dir->exists()) {
$directories = $dir->directories(false);
print_r($directories);
}
/* output
Array
(
[0] => packages\base\IO\directory\local Object
(
[directory] => packages/jalno/directory
[basename] => dir1
)
)
*/
Example 2 of correct code:
use packages\base\IO\Directory;
$dir = new Directory\Local("packages/jalno/directory");
if ($dir->exists()) {
$directories = $dir->directories(true);
print_r($directories);
}
/* output
Array
(
[0] => packages\base\IO\directory\local Object
(
[directory] => packages/jalno/directory
[basename] => dir1
)
[1] => packages\base\IO\directory\local Object
(
[directory] => packages/jalno/directory/dir1
[basename] => dir1.1
)
[2] => packages\base\IO\directory\local Object
(
[directory] => packages/jalno/directory/dir1/dir1.1
[basename] => dir1.1.1
)
)
*/
Example 3 of correct code
use packages\base\IO\Directory;
$ftp = new Directory\Ftp('public_html/packages/test/dir');
$ftp->hostname = "test.com";
$ftp->password = "123";
$ftp->username = "ali";
if ($ftp->exists()) {
$directories = $ftp->directories(false);
foreach ($directories as $directory) {
echo "directory: " . $directory->directory . " - basename: " . $directory->basename . "<br>";
}
}
/** output
* directory: public_html/packages/test/dir - basename: dir1
*/
Example 4 of correct code
use packages\base\IO\Directory;
$tmp = new Directory\Tmp();
$file = $tmp->file("newFile.txt");
$file->touch();
$dir = $tmp->directory("dir");
$dir->make();
print_r($tmp->directories());
/* output
Array
(
[0] => packages\base\IO\directory\local Object
(
[directory] => /tmp/L7lHwsjRp
[basename] => dir
)
)
*/
Example 5 of correct code
use packages\base\IO\Directory;
$sftp = new Directory\Sftp('public_html/packages/test/dir');
$sftp->hostname = "test.com";
$sftp->password = "123";
$sftp->username = "ali";
$sftp->port = 7624;
if ($sftp->exists()) {
$directories = $sftp->directories(false);
foreach ($directories as $directory) {
echo "directory: ".$directory->directory." - basename: ".$directory->basename."<br>";
}
}
/* output
directory: public_html/packages/test/dir - basename: dir1
*/
Listing the files in a directory
The files() method is used to get the list of files in a directory.
The input argument of this method is boolean.
If the value true is passed to the method, it also returns the inner files, and
if the value false is passed to the method, it only returns the files in the main directory. The output of this method is an array in which each element is an object of the File class.
The input argument is true by default.
If the directory does not exist, we receive a warning.
Example of erroneous code
use packages\base\IO\Directory;
$dir = new Directory\Local("packages/jalno/non-exists-directory");
$files = $dir->files(); // Warning: scandir(packages/jalno/non-exists-directory): failed to open dir: No such file or directory
Example 1
use packages\base\IO\Directory;
$dir = new Directory\Local("packages/jalno/directory");
if ($dir->exists()) {
$files = $dir->files(false);
print_r($files);
}
/* output
Array
(
[0] => packages\base\IO\file\local Object
(
[directory] => packages/jalno/directory
[basename] => file1
)
)
*/
Example 2
use packages\base\IO\Directory;
$dir = new Directory\Local("packages/jalno/directory");
if ($dir->exists()) {
$files = $dir->files(true);
print_r($files);
}
/* output
Array
(
[0] => packages\base\IO\file\local Object
(
[directory] => packages/jalno/directory/dir1/dir1.1
[basename] => file1.1
)
[1] => packages\base\IO\file\local Object
(
[directory] => packages/jalno/directory
[basename] => file1
)
)
*/
Example 3
use packages\base\IO\Directory;
$tmp = new Directory\Tmp();
$file = $tmp->file("newFile.txt");
$file->touch();
$dir = $tmp->directory("dir");
$dir->make();
print_r($tmp->files());
/* output
Array
(
[0] => packages\base\IO\file\local Object
(
[directory] => /tmp/ka3Dg5w4K
[basename] => newFile.txt
)
)
*/
Example 4
use packages\base\IO\Directory;
$ftp = new Directory\Ftp('public_html/packages/test/dir');
$ftp->hostname = "test.com";
$ftp->password = "123";
$ftp->username = "ali";
if ($ftp->exists()) {
$files = $ftp->files(false);
foreach ($files as $file) {
echo "directory: ".$file->directory." - basename: ".$file->basename."<br>";
}
}
/* output
directory: public_html/packages/test/dir - basename: file1
*/
Example 5
use packages\base\IO\Directory;
$sftp = new Directory\Sftp('public_html/packages/test/dir');
$sftp->hostname = "test.com";
$sftp->password = "123";
$sftp->username = "ali";
if ($sftp->exists()) {
$files = $sftp->files(false);
foreach ($files as $file) {
echo "directory: ".$file->directory." - basename: ".$file->basename."<br>";
}
}
/* output
directory: public_html/packages/test/dir - basename: file1
*/
Listing the items in a directory
The items() method is used to get the list of items in a directory.
The input argument of this method is boolean.
If the value true is passed to the method, it also returns the inner items, and
if the value false is passed to the method, it only returns the items in the main directory. The output of this method is an array of File and Directory objects.
The input argument is true by default.
If the directory does not exist, we receive a warning.
Example of erroneous code
use packages\base\IO\Directory;
$dir = new Directory\Local("packages/jalno/non-exists-directory");
$items = $dir->items(); // Warning: scandir(packages/jalno/non-exists-directory): failed to open dir: No such file or directory
Example 1 of correct code:
$dir = new IO\directory\local("packages/jalno/directory");
if ($dir->exists()) {
$items = $dir->items(false);
print_r($items);
}
/*output
Array
(
[0] => packages\base\IO\directory\local Object
(
[directory] => packages/jalno/directory
[basename] => dir1
)
[1] => packages\base\IO\file\local Object
(
[directory] => packages/jalno/directory
[basename] => file1
)
)
*/
Example 2 of correct code:
$dir = new IO\directory\local("packages/jalno/directory");
if ($dir->exists()) {
$items = $dir->items(true);
print_r($items);
}
/*output
Array
(
[0] => packages\base\IO\directory\local Object
(
[directory] => packages/jalno/directory
[basename] => dir1
)
[1] => packages\base\IO\directory\local Object
(
[directory] => packages/jalno/directory/dir1
[basename] => dir1.1
)
[2] => packages\base\IO\directory\local Object
(
[directory] => packages/jalno/directory/dir1/dir1.1
[basename] => dir1.1.1
)
[3] => packages\base\IO\file\local Object
(
[directory] => packages/jalno/directory/dir1/dir1.1
[basename] => file1.1
)
[4] => packages\base\IO\file\local Object
(
[directory] => packages/jalno/directory
[basename] => file1
)
)
*/
Example 3 of correct code:
use packages\base\IO\Directory;
$tmp = new Directory\Tmp();
$file = $tmp->file("newFile.txt");
$file->touch();
$dir = $tmp->directory("dir");
$dir->make();
print_r($tmp->items());
/* output
Array
(
[0] => packages\base\IO\directory\local Object
(
[directory] => /tmp/1iQGbNTY9
[basename] => dir
)
[1] => packages\base\IO\file\local Object
(
[directory] => /tmp/1iQGbNTY9
[basename] => newFile.txt
)
)
*/
Example 4 of correct code:
$ftp = new Directory\Ftp('public_html/packages/test/dir');
$ftp->hostname = "test.com";
$ftp->password = "123";
$ftp->username = "ali";
if ($ftp->exists()) {
$items = $ftp->items(false);
foreach ($items as $item) {
echo "directory: " . $item->directory . " - basename: " . $item->basename . "\n";
}
}
/* output
directory: public_html/packages/test/dir - basename: dir1
directory: public_html/packages/test/dir - basename: file1
*/
Example 5 of correct code:
$sftp = new Directory\Sftp('public_html/packages/test/dir');
$sftp->hostname = "test.com";
$sftp->password = "123";
$sftp->username = "ali";
$sftp->port = 7624;
if ($sftp->exists()) {
$items = $sftp->items(false);
foreach ($items as $item) {
echo "directory: " . $item->directory . " - basename: " . $item->basename . "\n";
}
}
/* output
directory: public_html/packages/test/dir - basename: dir1
directory: public_html/packages/test/dir - basename: file1
*/