Search code examples
phpphar

Strange error message with Phar


Using phar to create tar.gz archive returns a strange error, the following is the error message:

exception 'BadMethodCallException' with message 'Unable to add newly converted phar "c:/www/dimg/uploads/7e6d3a5e39e43d1351e7069517f11250.tar.gz" to the list of phars, a phar with that name already exists' in c:\www\dimg\upload.php:163 Stack trace:

0 c:\www\dimg\upload.php(163): PharData->compress(4096)

1 {main}

The snippet to produce Phar archives am using:

$dir_id        = md5(microtime() . $_SERVER['REMOTE_ADDR']);
$upload_dir    = 'uploads/' . $dir_id;
mkdir($upload_dir, 777);

try {
    $a = new PharData($upload_dir . '.tar.gz');
    $a->buildFromDirectory($upload_dir);
    $a->compress(Phar::GZ);
} catch (Exception $e) {
    $error = true;
    $err_msg .= '<li>Exception : ' . $e . '</li>';
}

I tried to empty uploads directory but the same error is produced each time.


Solution

  • Instead of using

    $a = new PharData($upload_dir . '.tar.gz');
    

    Use:

    $a = new PharData($upload_dir . '.tar');
    

    Actually compress generates two .tar and then tar.gz. Since you have specified .tar.gz as the initial output, it cannot overwrite it with the same file type.