Skip to main content
Version: 2.0.0

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 nameDirectory storage location
packages\base\IO\Directory\LocalLocal directories
packages\base\IO\Directory\TmpTemporary local directories
packages\base\IO\Directory\FtpRemote directories
packages\base\IO\Directory\SftpRemote directories
note

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 nameDescription
copyTo(directory $dest): boolCopies the contents of one directory into another directory
copyFrom(directory $source): boolCopies contents from another directory
delete(): mixedDeletes the directory from disk
rename(string $newName): boolRenames
move(directory $dest): boolMoves a directory to another directory
make(): boolCreates the directory
files(bool $recursively): arrayLists the files in the directory
items(bool $recursively): arrayLists the items in the directory
directories(bool $recursively): arrayLists the directories in the directory
file(string $name): FileAssigns a file to the directory
directory(string $name): DirectoryAssigns a directory to the directory
size(): intCalculates the size of a directory in bits
getPath(): stringGets the path of a directory
isEmpty(): boolIndicates whether the directory is empty or not
getDirectory(): DirectoryProvides access to the folder containing this directory.
exists(): boolChecks whether the directory exists

Variables

NameDescription
basenameThe name of the folder in which it is stored.
directoryThe path of the folder in which it is stored.
note

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 nameDescription
getRealPath(): stringGets the exact path of a directory
note

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:

controllers/Directories.php
<?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.

note

Every temporary directory is also a local directory and has all the capabilities and abilities of packages\base\IO\Directory\Local.

note

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:

controllers/Directories.php
<?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";
note

If the entered server credentials are incorrect, we receive Warning: ftp_connect(): php_network_getaddresses: getaddrinfo failed: Name or service not known.

Example 2:

controllers/Directories.php
<?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;
note

If the entered server credentials are incorrect, we receive exception: packages\base\ssh\ConnectionException.

Example 2:

controllers/Directories.php
<?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.

note

The default value of the make() method's input argument is false.

note

If the value false is passed to the method and its parent directory does not exist, we receive a warning.

note

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.

note

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.

note

If the source directory does not exist, we receive a warning.

note

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.

note

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.

note

If the destination directory does not exist, it is created.

note

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.

note

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.

note

If the directory does not exist, we receive a warning.

note

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.

note

The input argument is true by default.

note

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.

note

The input argument is true by default.

note

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.

note

The input argument is true by default.

note

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
*/