[英]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.