问题描述
My Phar脚本使用fwrite创建一个新文件,该文件工作正常,它在phar之外创建新文件,与phar文件位于同一目录中。
My Phar script creates a new file with fwrite, which works fine, it creates the new file outside the phar, in the same directory as the phar file.
但是当我使用if(file_exists('file.php'))时它不会把它拿起来。
But then when i use if(file_exists('file.php')) it doesn't pick it up.
但是然后包括并且要求捡起它。
But then include and require do pick it up.
任何人都知道这个问题?
经过一段时间的测试和研究似乎无法找到解决方案。
Anyone know about this problem?Been testing and researching for a while a can't seem to find a solution.
推荐答案
在PHAR的存根,你可以使用 __ DIR __
魔术常量来获取PHAR文件的文件夹。
At the PHAR's stub, you can use the __DIR__
magic constant to get the PHAR file's folder.
考虑到这一点,你可以只需使用
With that in mind, you can simply use
is_file(__DIR__ . DIRECTORY_SEPARATOR . $path);
检查文件是否存在于PHAR之外。
To check for a file's existence outside the PHAR.
只能从存根执行此操作,并且只有它是自定义存根,而不是由Phar :: setDefaultStub()生成的存根。如果你需要检查更下面的文件,你必须以某种方式使该常量值可用,如全局变量,自定义非魔法常量或静态属性或其他文件,然后咨询。
You can ONLY do this from the stub, and ONLY if it's a custom stub, as opposed to one generated by Phar::setDefaultStub(). If you need to check for files further down the line, you'll have to make that constant's value available somehow, like a global variable, a custom non-magical constant or a static property or something, which other files then consult with.
编辑:实际上,你也可以使用 dirname(Phar :: running(false))
来获取PHAR的文件夹来自PHAR的任何地方。如果你不在PHAR中,那个函数会返回一个空字符串,所以无论你的应用程序是作为PHAR执行还是直接执行,它都可以正常工作,例如
Actually, you can also use dirname(Phar::running(false))
to get the PHAR's folder from anywhere in the PHAR. That function returns an empty string if you're not within a PHAR, so whether your application is executed as a PHAR or directly, it should work fine, e.g.
$pharFile = Phar::running(false);
is_file(('' === $pharFile ? '' : dirname($pharFile) . DIRECTORY_SEPARATOR) . $path)
这篇关于PHP Phar - file_exists()问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!