简体   繁体   English

如何在phar文件代码中获取.phar文件真实目录?

[英]how to get a .phar file real directory within the phar file code?

I am trying to create a php executable (a phar file) for generating some files, and I would like to know how to get the real path of the phar file (within the phar file code). 我正在尝试创建一个php可执行文件(一个phar文件)来生成一些文件,我想知道如何获取phar文件的真实路径(在phar文件代码中)。

What I want to do is to create a folder in the same level of the phar file and create the new files there, but realpath(__DIR__.'/../') does not seem to work. 我想要做的是在phar文件的同一级别创建一个文件夹并在那里创建新文件,但realpath(__DIR__.'/../')似乎不起作用。

Thanks 谢谢

As shown in https://stackoverflow.com/a/28775172/282601 the __FILE__ constant has the full path with the scheme: https://stackoverflow.com/a/28775172/282601所示, __FILE__常量具有该方案的完整路径:

phar:///home/cweiske/Dev/test/phar/test.phar/path/to/foo.php

__DIR__ is similar: __DIR__类似:

phar:///home/cweiske/Dev/test/phar/test.phar/path/to

So when calling realpath(__DIR__) you still have the phar:// prefix that prevents you from loading the file. 所以当调用realpath(__DIR__)你仍然有phar://前缀,阻止你加载文件。

You have to remove the phar:// scheme as well as the path of the file inside the .phar to get the phar location with __DIR__ . 您必须删除phar:// scheme以及.phar文件的路径以获取具有__DIR__的phar位置。


Much easier is Phar::running(false) , which simply returns the path: 更简单的是Phar::running(false) ,它只返回路径:

/home/cweiske/Dev/test/phar/test.phar

the answer is 答案是

 $string = 'phar://E:/php/www/my.phar';
 $string = pathinfo($string);
 $_dir_ = parse_url($string['dirname']);
 echo $_dir_['host'].':/'.$_dir_['path'];

result 结果

E:/php/www

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM