What potential error can occur when using the scandir() function in PHP?

When using the scandir() function in PHP, one potential error that can occur is that it may return false if the specified directory cannot be opened or read. To handle this error, you should check if scandir() returns false before proceeding with further operations to avoid unexpected behavior or errors in your code.

$directory = 'path/to/directory';

// Check if scandir() returns false before proceeding
if ($files = scandir($directory)) {
    // Continue with further operations
    foreach ($files as $file) {
        // Process each file in the directory
    }
} else {
    echo 'Error: Unable to read directory';
}