How can the realpath() function be utilized to address path issues when working with ZipArchive in PHP?

When working with ZipArchive in PHP, path issues may arise when specifying file paths. To address this, the realpath() function can be used to get the absolute path of a file, ensuring that the correct path is used when adding files to or extracting files from a zip archive.

$zip = new ZipArchive;
$zipFileName = 'example.zip';
$zip->open($zipFileName, ZipArchive::CREATE);

$fileToAdd = 'example.txt';
$realFilePath = realpath($fileToAdd);

$zip->addFile($realFilePath);

$zip->close();