I have two files with the paths:
/reader.php
/ebook/titlepage.xhtml
I plan for the reader to eventually read the ebooks toc.ncx and be able to display this in a left hand pane for easy navigation. However for now, all I want to do is get around my problem with displaying the xhtml file.
The ebook titlepage starts like:
<?xml version='1.0' encoding='utf-8'?>
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en">
<head>
The problem is when I use
include(/ebook/titlepage.xhtml);
I get unexpected version T_String because my server is set up to use the short open tag
I tried to use ini_set('short_open_tag', '0') before my include to no avail
Then I tried readfile('ebook/titlepage.xhtml') which works except the images are referenced relative from reader.php rather than relevant from the titlepage.xhtml.
I do not want to edit any of the ebooks files so using echo <?xml... is out the question; I eventually wish to have all the file packaged in ebook.epub and to try and use php to navigate the archive to read and display the files in my reader
I suppose my question is, how can I get read file to use paths relative to ebook without editing any of the ebbok files? or how can I use include and ignore
Or, perhaps there is a better way that I am unaware of here. Perhaps a way to load the html file and process only the stuff within or just the body, or the head and body since I would like to make use of the styles and titles in head eventually.
Update (Solved) using pasfree’s answer
I now load the HTML into a domdocument and add a base tag to the head before outputting it using savehtml
//Load the ebook title page
$doc = new DOMDocument();
$doc->loadHTMLFile("{$_GET["book"]}/titlepage.xhtml");
//Create the base element
$base = $doc->createElement("base");
$baseHref = $doc->createAttribute('href');
$baseHref->value = "{$_GET["book"]}/";
$base->appendChild($baseHref);
//add <base...> to <head>
$head = $doc->getElementsByTagName("head")->item(0);
$head->appendChild($base);
//output the page
echo $doc->saveHTML();
Thanks
readfile() is right for inserting the files content.
You can use file_get_contents() too.
A good practice, is to set a global base href in your html docs so the relative paths will be resolved correctly.
https://developer.mozilla.org/en-US/docs/HTML/Element/base