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

编程入门 行业动态 更新时间:2024-10-25 16:17:03
本文介绍了如何在phar文件代码中获取.phar文件真实目录?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述

我试图创建一个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.

谢谢

推荐答案

如 stackoverflow/a/28775172/282601 __FILE__常量具有以下方案的完整路径:

As shown in stackoverflow/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文件真实目录?

本文发布于:2023-06-04 08:32:03,感谢您对本站的认可!
本文链接:https://www.elefans.com/category/jswz/34/493302.html
版权声明:本站内容均来自互联网,仅供演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,我们将在24小时内删除。
本文标签:文件   真实   代码   目录   如何在

发布评论

评论列表 (有 0 条评论)
草根站长

>www.elefans.com

编程频道|电子爱好者 - 技术资讯及电子产品介绍!