开发者

PHP absolute path to file 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)) ? ar开发者_StackOverflow中文版ray_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.


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.


have you tried realpath?


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜