Creating Directories - machitgarha/pusheh GitHub Wiki
You can create directories using Pusheh, giving the directory path you want you create:
Pusheh::createDir(__DIR__ . "/src");
Directory mode
You can also set directory mode when creating it. Directory mode is something like access mode or permission status, which determines who can access the directory for reading, writing or executing. From the Linux man page of chmod
:
A numeric mode is from one to four octal digits (0-7), derived by adding up the bits with values 4, 2, and 1. Omitted digits are assumed to be leading zeros. The first digit selects the set user ID (4) and set group ID (2) and restricted deletion or sticky (1) attributes. The second digit selects permissions for the user who owns the file: read (4), write (2), and execute (1); the third selects permissions for other users in the file's group, with the same values; and the fourth for other users not in the file's group, with the same values.
Now, it's the time for an example:
Pusheh::createDir(__DIR__ . "/app/bin", 0755);
NOTE: The above method will throw an exception if the app
directory, for example, is not present. To prevent this behavior and create directories one-by-one, read the next section.
Creating directories recursively
Do you want to create nested directories?
Pusheh::createDir(__DIR__ . "/app/bin", 0777, true);
// Better syntax
Pushes::createDirRecursive(__DIR__ . "/app/bin");
// Create directories recursively with the specified mode
Pushes::createDirRecursive(__DIR__ . "/app/bin", 0755);
What the code does is if the app
directory doesn't exist, create it before creating bin
directory. This is useful when you want to create an in-depth directory in one go.
Return values and exceptions
Both Pusheh::createDir()
and Pusheh::createDirRecursive()
methods will return true if the directory(s) created successfully and will return false if the directory(s) exists. As explained above, in Pusheh::createDir()
, an exception will be thrown if one of the base directories does not exist.