Directories in PHP

Here’s the translation of the Go code to PHP, with explanations in Markdown format suitable for Hugo:

Our first example demonstrates how to work with directories in PHP. We’ll explore various functions for creating, listing, and navigating directories.

<?php

// Helper function to check for errors
function check($result) {
    if ($result === false) {
        throw new Exception("An error occurred");
    }
}

// Create a new sub-directory in the current working directory
check(mkdir("subdir", 0755));

// When creating temporary directories, it's good practice to remove them afterwards
register_shutdown_function(function() {
    system('rm -rf subdir');
});

// Helper function to create a new empty file
function createEmptyFile($name) {
    check(file_put_contents($name, ""));
}

createEmptyFile("subdir/file1");

// We can create a hierarchy of directories, including parents
check(mkdir("subdir/parent/child", 0755, true));

createEmptyFile("subdir/parent/file2");
createEmptyFile("subdir/parent/file3");
createEmptyFile("subdir/parent/child/file4");

// scandir lists directory contents
$contents = scandir("subdir/parent");
check($contents);

echo "Listing subdir/parent\n";
foreach ($contents as $entry) {
    if ($entry != "." && $entry != "..") {
        echo " ", $entry, " ", is_dir("subdir/parent/$entry") ? "true" : "false", "\n";
    }
}

// chdir lets us change the current working directory
check(chdir("subdir/parent/child"));

// Now we'll see the contents of subdir/parent/child when listing the current directory
$contents = scandir(".");
check($contents);

echo "Listing subdir/parent/child\n";
foreach ($contents as $entry) {
    if ($entry != "." && $entry != "..") {
        echo " ", $entry, " ", is_dir($entry) ? "true" : "false", "\n";
    }
}

// Change back to where we started
check(chdir("../../.."));

// We can also visit a directory recursively, including all its sub-directories
echo "Visiting subdir\n";
$iterator = new RecursiveIteratorIterator(new RecursiveDirectoryIterator("subdir"));
foreach ($iterator as $path => $fileInfo) {
    if (!$fileInfo->isDir()) {
        echo " ", $path, " false\n";
    } else {
        if ($fileInfo->getFilename() != '.' && $fileInfo->getFilename() != '..') {
            echo " ", $path, " true\n";
        }
    }
}

To run the program, save it as directories.php and use php:

$ php directories.php
Listing subdir/parent
 child true
 file2 false
 file3 false
Listing subdir/parent/child
 file4 false
Visiting subdir
 subdir true
 subdir/file1 false
 subdir/parent true
 subdir/parent/child true
 subdir/parent/child/file4 false
 subdir/parent/file2 false
 subdir/parent/file3 false

This PHP script demonstrates various operations with directories:

  1. We use mkdir() to create directories.
  2. file_put_contents() is used to create empty files.
  3. scandir() is used to list directory contents.
  4. chdir() allows us to change the current working directory.
  5. We use RecursiveIteratorIterator and RecursiveDirectoryIterator to recursively visit all files and directories.

Note that PHP doesn’t have a direct equivalent to Go’s defer for cleanup, so we use register_shutdown_function() to ensure our temporary directory is removed at the end of the script execution.

Also, unlike Go, PHP doesn’t have a built-in way to get both the name and type of directory entries in a single call. We need to use is_dir() separately to check if an entry is a directory.