phpsymfonypharsymfony-filesystem

Phar archive no read and write permissions with symfony filesystem


i have created a cli app which tries to create a file with symfony 3 fileSystem component. All works fine, but if i put the app in a phar archive symfony can't write the file.

This is how i created the phar archive:

<?php
$phar = new Phar('app.phar', 0, 'app.phar');
$phar->buildFromDirectory(dirname(__FILE__) );
$phar->setStub($phar->createDefaultStub('app.php'));

The app.php call the symfony filesystem component:

<?php 

....

$this->fileSystem->dumpFile(
    'testfile.txt',
    'my test content'
);

This works fine if i execute app.php directly. But if i execute it as a .phar i get this message.

[Symfony\Component\Filesystem\Exception\IOException]  
Failed to create ".": mkdir(): File exists.

Other operations like echo working fine. In my opinion i think, that the phar archive has a problem with IO operations. But i don't unterstand whats the problem. In my php.ini is phar.readonly=Off configured.

I hope someone can give me a hint. :)


Solution

  • Phar's have a different path structure, and that symfony method does dirname($filename); https://github.com/symfony/symfony/blob/master/src/Symfony/Component/Filesystem/Filesystem.php#L679

    Try defining the full path.

    $path = '.';
    if (!empty($pharPath = \Phar::running(false))) {
        $path = dirname($pharPath);
    } 
    
    $this->fileSystem->dumpFile(
        $path.'/testfile.txt',
        'my test content'
    );