问题描述
我试图创建一个php可执行文件(一个phar文件)以生成一些文件,并且我想知道如何获取phar文件的真实路径(在phar文件代码内).
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).
我想做的是在phar文件的同一级别中创建一个文件夹,然后在其中创建新文件,但是realpath(__DIR__.'/../')
似乎不起作用.
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.
谢谢
推荐答案
如 https://stackoverflow.com/a/28775172/282601 __FILE__
常量具有以下方案的完整路径:
As shown in https://stackoverflow.com/a/28775172/282601 the __FILE__
constant has the full path with the scheme:
phar:///home/cweiske/Dev/test/phar/test.phar/path/to/foo.php
__DIR__
与之类似:
phar:///home/cweiske/Dev/test/phar/test.phar/path/to
因此,在调用realpath(__DIR__)
时,您仍然具有phar://
前缀,可防止您加载文件.
So when calling realpath(__DIR__)
you still have the phar://
prefix that prevents you from loading the file.
您必须删除phar://
方案以及.phar
内部文件的路径,才能使用__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::running(false)
容易得多,它只是返回路径:
Much easier is Phar::running(false)
, which simply returns the path:
/home/cweiske/Dev/test/phar/test.phar
这篇关于如何在phar文件代码中获取.phar文件真实目录?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!