Introduction
I think this basically has to do with the fact that you are working with Phar::TAR
files because
file_put_contents("phar://./test.tar/foo.txt", "hello world");
^
|+---- Note this tar
Returns
Warning: file_put_contents(phar://./test.tar/foo.txt): failed to open stream: Cannot create phar './test.tar', file extension (or combination) not recognized or the directory does not exist
While
file_put_contents("phar://./test.phar/foo.txt", "hello world"); // Works file
^
|+---- Note This phar
Returns
//No Errors
Know Issue
If you look at a detail example in the PHP doc , this has been a known issue for the past 2 years
and the example given are as follows
Example
$p = new PharData(dirname(__FILE__) . '/phartest.zip', 0, 'phartest', Phar::ZIP);
$p->addFromString('testfile.txt', 'this is just some test text');
// This works
echo file_get_contents('phar://phartest.zip/testfile.txt');
// This Fails
file_put_contents('phar://phartest.zip/testfile.txt', 'Thist is text for testfile.txt');
$context = stream_context_create(array(
'phar' => array(
'compress' => Phar::ZIP
)
));
// This Fails
file_put_contents('phar://phartest.zip/testfile.txt', 'Thist is text for testfile.txt', 0, $context);
// This works but only with 'r' readonly mode.
$f = fopen('phar://C:\\Inetpub\\wwwroot\\PACT\\test\\phartest.zip\\testfile.txt', 'r');
Working Alternative
Don't use .tar
or .zip
has your file extension
but set compression as demonstrated in the PHP DOC
What do i mean ?
$context = stream_context_create(array(
'phar' => array(
'compress' => Phar::GZ //set compression
)
));
file_put_contents('phar://sample.phar/somefile.txt', "Sample Data", 0, $context);
^
|= Use phar not tar
Advice
I really think you should stick with PharData
is realizable and proven to work. The only way to make
I must use file_put_contents
Then write your own wrapper and register it with stream_wrapper_register
here is a simple Prof of Concept
stream_wrapper_register("alixphar", "AlixPhar");
file_put_contents('alixphar://phar.tar/test/foo.txt', 'hey');
^
|+-- Write with alixphar
echo file_get_contents('alixphar://phar.tar/test/foo.txt'),PHP_EOL;
echo file_get_contents('phar://phar.tar/test/foo.txt'),PHP_EOL;
Output
hey <----- alixphar
hey <----- phar
Note: This class should not be used in production ... It just an Example and would fail some test cases
class AlixPhar {
private $pos;
private $stream;
private $types;
private $dir;
private $pharContainer, $pharType, $pharFile, $pharDir = null;
private $fp;
function __construct() {
$this->types = array(
"tar" => Phar::TAR,
"zip" => Phar::ZIP,
"phar" => Phar::PHAR
);
// your phar dir to help avoid using path before the phar file
$this->dir = __DIR__;
}
private function parsePath($path) {
$url = parse_url($path);
$this->pharContainer = $url['host'];
$this->pharType = $this->types[pathinfo($this->pharContainer, PATHINFO_EXTENSION)];
if (strpos($url['path'], ".") !== false) {
list($this->pharDir, $this->pharFile, , ) = array_values(pathinfo($url['path']));
} else {
$this->pharDir = $url['path'];
}
}
public function stream_open($path, $mode, $options, &$opened_path) {
$this->parsePath($path);
$this->stream = new PharData($this->dir . "/" . $this->pharContainer, 0, null, $this->pharType);
if (! empty($this->pharDir))
$this->stream->addEmptyDir($this->pharDir);
if ($mode == "rb") {
$this->fp = fopen("phar://" . $this->pharContainer . "/" . $this->pharDir . "/" . $this->pharFile, $mode);
}
return true;
}
public function stream_write($data) {
$this->pos += strlen($data);
$this->stream->addFromString($this->pharDir . "/" . $this->pharFile, $data);
return $this->pos;
}
public function stream_read($count) {
return fread($this->fp, $count);
}
public function stream_tell() {
return ftell($this->fp);
}
public function stream_eof() {
return feof($this->fp);
}
public function stream_stat() {
return fstat($this->fp);
}
}