'PharData to compress file to tar.gz file is showing file already exists even after unlink the file

I am trying to create a tar.gz file from an existing folder. My steps are

  1. Untar the existing file.
  2. Update the contents of some files.
  3. Compress it again to tar.gz file

I am trying the code

if(unlink("path/to/my/tar.gz file")){
    $phar = new PharData("path/to/filename.tar");
    $phar->buildFromDirectory("path/to/directory");
    if("path/to/tar.gz file")){
        $phar->compress(Phar::GZ);
    }
}
unlink("path/to/filename.tar");

But once I run the code, it showing error in the line

$phar->compress(Phar::GZ);

The error is

Unable to add newly converted phar "path/to/tar.gz file" to the list of phars, a phar with that name already exists in /path/to/php file

Why this issue is coming? I am already unlinking and compressing only if the file doesn't exist on the path.

Can someone please give a solution for this issue?

Thanks in advance



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source