How can the use of `chdir` be beneficial in managing file paths when creating zip files in PHP scripts?

When creating zip files in PHP scripts, managing file paths can be cumbersome. Using the `chdir` function can be beneficial as it allows you to change the current directory, making it easier to navigate and access files for zipping. By changing the directory to where the files are located, you can simplify the process of adding them to the zip archive without having to specify the full path for each file.

<?php

// Set the directory where the files are located
$directory = '/path/to/files';

// Change the current directory to the specified directory
chdir($directory);

// Create a new ZipArchive object
$zip = new ZipArchive();

// Specify the name of the zip file to create
$zipName = 'archive.zip';

// Open the zip file for writing
if ($zip->open($zipName, ZipArchive::CREATE) === TRUE) {
    // Add all files in the current directory to the zip archive
    $files = glob('*');
    foreach ($files as $file) {
        $zip->addFile($file);
    }
    // Close the zip file
    $zip->close();
    echo 'Zip file created successfully';
} else {
    echo 'Failed to create zip file';
}

?>