Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Strange error message with Phar

Tags:

php

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.

like image 970
H Aßdøµ Avatar asked Aug 25 '26 03:08

H Aßdøµ


2 Answers

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.

like image 83
Ehsan Avatar answered Aug 27 '26 17:08

Ehsan


I believe the problem is that you are trying to build the directory that the tar.gz is being sent to. In other words, you are telling it to build to a particular directory, and then telling it to build that directory.

All you should need to do is build the tar.gz in another directory than the one you are trying to compress.

like image 44
Ramsay Smith Avatar answered Aug 27 '26 17:08

Ramsay Smith