English 中文(简体)
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)) ? 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.

问题回答

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.





相关问题
Brute-force/DoS prevention in PHP [closed]

I am trying to write a script to prevent brute-force login attempts in a website I m building. The logic goes something like this: User sends login information. Check if username and password is ...

please can anyone check this while loop and if condition

<?php $con=mysql_connect("localhost","mts","mts"); if(!con) { die( unable to connect . mysql_error()); } mysql_select_db("mts",$con); /* date_default_timezone_set ("Asia/Calcutta"); $date = ...

定值美元

如何确认来自正确来源的数字。

Generating a drop down list of timezones with PHP

Most sites need some way to show the dates on the site in the users preferred timezone. Below are two lists that I found and then one method using the built in PHP DateTime class in PHP 5. I need ...

Text as watermarking in PHP

I want to create text as a watermark for an image. the water mark should have the following properties front: Impact color: white opacity: 31% Font style: regular, bold Bevel and Emboss size: 30 ...

How does php cast boolean variables?

How does php cast boolean variables? I was trying to save a boolean value to an array: $result["Users"]["is_login"] = true; but when I use debug the is_login value is blank. and when I do ...

热门标签