PHP 文件 URI 的绝对路径

发布于 2024-08-11 06:44:37 字数 998 浏览 6 评论 0原文

为了在使用 PHP SimpleXML 时引用本地 DTD,我需要将绝对路径转换为文件类型 URI。例如,将 /home/sitename/www/xml/example.dtd 转换为 file:///home/sitename/www/xml/example.dtd。

在给出的示例中,这很简单,因为所需要做的就是在路径前面添加“文件”方案。但如果出现诸如其中一个目录名中有空格之类的情况,这就不够好了。该机制应该在 Windows 或 Linux 上运行,并允许目录名称中使用非 ASCII 字符。

到目前为止设计的代码是:

    $absparts = explode('/', _ALIRO_ABSOLUTE_PATH);
    $driveletter = (0 == strncasecmp(PHP_OS, 'win', 3)) ? array_shift($absparts) : '';
    $filename = $driveletter.implode('/', array_map('rawurlencode', $absparts)).'/xml/'.$filename.'.dtd';
    $href = 'file:///'.$filename;

其中定义的符号是系统根目录的绝对路径(始终带有正斜杠),DTD 位于 xml 子目录中,名称为 $filename,后跟扩展名 .dtd。

这能正常工作吗?有更好的方法吗?

让我解释一下背景。目的是使用 SimpleXML 解析 XML 文档。这是使用 simplexml_load_string() 函数和 LIBXML_DTDVALID 选项来完成的。 XML 文档将包含指向主网站的真实 URI,但我不想在到达远程网站或通过请求加载主网站时引入延迟。因此,编辑对 DTD 的引用以引用本地计算机。但它必须作为 URI 嵌入 XML 文档内的 DOCTYPE 中。这些约束不是我的选择,它们隐含在 DOCTYPE 规则中,并由 SimpleXML 函数强制执行。我可以通过绝对路径计算出文件所在的位置,但要将其放入 DOCTYPE,必须将其转换为 URI。

In order to refer to a local DTD when using PHP SimpleXML, I need to convert the absolute path into a file type URI. For example, convert /home/sitename/www/xml/example.dtd to file:///home/sitename/www/xml/example.dtd.

In the example given, it is easy enough, since all that is required is to add the 'file' scheme in front of the path. But if a situation arises such as there being a blank in one of the directory names, this is not good enough. The mechanism should work on Windows or Linux, and allow for non-ASCII characters in the directory names.

The code devised so far is:

    $absparts = explode('/', _ALIRO_ABSOLUTE_PATH);
    $driveletter = (0 == strncasecmp(PHP_OS, 'win', 3)) ? array_shift($absparts) : '';
    $filename = $driveletter.implode('/', array_map('rawurlencode', $absparts)).'/xml/'.$filename.'.dtd';
    $href = 'file:///'.$filename;

where the defined symbol is the absolute path to the system root (always with forwards slashes), the DTD is in the xml subdirectory, and has a name of $filename followed by the extension .dtd.

Will this work correctly? Is there a better approach?

Let me explain a little more background. The aim is to parse an XML document using SimpleXML. This is done using the simplexml_load_string() function with the LIBXML_DTDVALID option. The XML document will contain a real URI pointing to a home web site, but I do not want to introduce delays involved in reaching a distant web site, or load up the home web site with requests. The reference to the DTD is therefore edited so as to refer to the local machine. But it has to be embedded as a URI within a DOCTYPE inside the XML document. The constraints are not my choice, they are implicit in the rules for a DOCTYPE and are enforced by the SimpleXML function. I can work out where the file is located as an absolute path, but to put it into a DOCTYPE, it must be converted into a URI.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

七月上 2024-08-18 06:44:37

如果您在本地运行 PHP,则必须运行 Apache 之类的服务器。那么为什么需要 file:// 引用呢?只需使用常规参考即可。

如果您的脚本位于 http://localhost/index.php 上,您可以在 HTML 中将该文件引用为 /xml/example.dtd。或者,如果您想从 PHP 读取文件,请使用 $_SERVER['DOCUMENT_ROOT'] 。 '/xml/example.dtd'

在这些情况下,相同的代码应该可以在本地计算机和实时服务器上正常工作。


好的,我根据您澄清的问题进行了思考,您可能做得超出了您的需要。我不相信您需要检测 Windows,您只需在文档根目录前添加 file:// 前缀(并对 URL 进行编码)。换句话说,您最终会得到 file://C:/My Documents...file:///home/site...

的当然,您仍然可以使用 HTTP 引用来代替文件。就像我上面说的,用户将在服务器上运行,因此您应该能够使用 $_SERVER 变量的各个部分(例如 $_SERVER['HTTP_HOST']) 拼凑出更具体的 URL。

If you're running PHP locally, you must be running a server like Apache. So why the need for the file:// reference? Just use a regular reference.

If your script is on http://localhost/index.php you can refer to the file as /xml/example.dtd in your HTML. Or, if you mean to read the file from PHP, use $_SERVER['DOCUMENT_ROOT'] . '/xml/example.dtd'

In these cases the same code should work fine on your local machine and on the live server.


OK I had a think based on your clarified question, and you're probably doing more than you need. I'm not convinced you need to detect Windows, you just need to prefix your document root with file:// (and encode the URL). In other words, you'd end up with either file://C:/My Documents... or file:///home/site...

Of course, you could still use a HTTP reference instead of file. Like I say above, the user will be running on a server so you should be able to use the various parts of the $_SERVER variable (e.g. $_SERVER['HTTP_HOST']) to piece together a more concrete URL.

瞎闹 2024-08-18 06:44:37

您尝试过 realpath 吗?

have you tried realpath?

维持三分热 2024-08-18 06:44:37

您真的需要将其本地化吗?不能将 dtd 托管在 Web 服务器上吗?显然,这使得它变得更加容易,并且您无需担心转换。我认为您最好尝试托管它,而不是处理操作系统差异。

Do you really need to make it local? Can't you host the dtd on the web server? Obviously that makes it much easier and you don't need to worry about conversion. I think you would be better off trying to get it hosted rather than deal with the OS differences.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文