当我尝试这个:
$phar = new PharData('./phar.tar', 0, null, Phar::TAR);
$phar->addEmptyDir('test');
file_put_contents('phar://./phar.tar/test/foo.txt', 'bar');
Run Code Online (Sandbox Code Playgroud)
我收到以下错误:
警告:file_put_contents(phar://./phar.tar/test/foo.txt):无法打开流:无法创建phar'./ phar.tar',文件扩展名(或组合)无法识别或目录不存在
我可以使用file_get_contents但不应该使用file_put_contents吗?
为了让事情更加怪异,我尝试了@hakre在他的评论中提出的想法并且它有效!
$phar = new PharData('./phar.tar', 0, null, Phar::TAR);
$phar->addEmptyDir('test');
if (file_exists('./phar.phar') !== true)
{
symlink('./phar.tar', './phar.phar');
}
file_put_contents('phar://./phar.phar/test/foo.txt', 'bar');
var_dump(file_get_contents('phar://./phar.tar/test/foo.txt')); // string(3) "bar"
Run Code Online (Sandbox Code Playgroud)
介绍
我认为这基本上与您使用Phar::TAR文件的事实有关
file_put_contents("phar://./test.tar/foo.txt", "hello world");
^
|+---- Note this tar
Run Code Online (Sandbox Code Playgroud)
返回
警告:file_put_contents(phar://./test.tar/foo.txt):无法打开流:无法创建phar'./ test.tar',文件扩展名(或组合)无法识别或目录不存在
而
file_put_contents("phar://./test.phar/foo.txt", "hello world"); // Works file
^
|+---- Note This phar
Run Code Online (Sandbox Code Playgroud)
返回
//No Errors
Run Code Online (Sandbox Code Playgroud)
知道问题
如果您查看PHP文档中的详细示例,这已成为过去的已知问题,2 years给出的示例如下所示
例
$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');
Run Code Online (Sandbox Code Playgroud)
工作选择
不要使用.tar或.zip拥有您的文件,extension而是设置压缩,如PHP DOC中所示
我的意思是什么?
$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
Run Code Online (Sandbox Code Playgroud)
忠告
我真的认为你应该坚持下去PharData是可以实现的,并证明是有效的.唯一的方法
我必须使用file_put_contents
然后编写自己的包装器并stream_wrapper_register在这里注册它是一个简单的概念教授
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;
Run Code Online (Sandbox Code Playgroud)
产量
hey <----- alixphar
hey <----- phar
Run Code Online (Sandbox Code Playgroud)
注意:此类不应在生产中使用...它只是一个示例,并且会使某些测试用例失败
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);
}
}
Run Code Online (Sandbox Code Playgroud)
我有一些答案给你 - 希望这会对你有所帮助。
首先,使用 file_put_contents(),我注意到一些奇怪的地方。首先,我所做的是按照您发布的链接的示例进行操作。我能够添加 $context 变量,但仍然有错误。对我来说效果好的是将文件名更改为 phar.phar。也许 phar:// 处理程序无法理解 .tar 扩展名?
无论如何,这似乎是有效的代码 -但我不推荐它
$context = stream_context_create(array('phar' =>
array('compress' => Phar::GZ)),
array('metadata' => array('user' => 'cellog')));
file_put_contents('phar://./phar.phar/test/foo.txt', 'bar', 0, $context);
Run Code Online (Sandbox Code Playgroud)
相反,使用这个。
使用我 - 我被推荐
$phar = new PharData('./phar.tar', 0, null, Phar::TAR);
$phar->addEmptyDir('test');
$phar->addFile('./bar', 'foo.txt');
Run Code Online (Sandbox Code Playgroud)
如果您确实需要使用 file_put_contents() 代替,也许存在其他问题,我们可以帮助解决。谢谢,祝你好运!