What are alternative methods or functions in PHP for performing file system operations like formatting without relying on system commands?

When performing file system operations in PHP, it is best to avoid relying on system commands for security and portability reasons. Instead, PHP provides built-in functions for handling file system operations like formatting. One alternative method is to use the `ftruncate()` function to truncate a file to a specified length, effectively "formatting" the file by removing its contents.

$file = 'example.txt';

// Open the file for writing
$handle = fopen($file, 'w');

// Truncate the file to zero length
if (ftruncate($handle, 0)) {
    echo "File $file has been formatted.";
} else {
    echo "Failed to format file $file.";
}

// Close the file handle
fclose($handle);