在PHP中从私人文件夹渲染HTML页面

I have a web site that have HTML pages stored in a private folder. I want a PHP script that can read the HTML file then push it to the browser.

My tought was to get the html file with the file() function in PHP. Then echo() it to the browser. That works for the html content of the page. The images and the css does not follow however.

I heard of a "render" function in IIS or ASP that render the HTML content of a web page in a private folder then send the images in a binary format. Does PHP have something similar?

Currently I read the file as follow :

$htmlFile = file(PATHTOFILE);
echo(implode('',$htmlFile));

The reason we are trying to do that is to protect the url / information of the pages contained in this folder. The user will have to connect to the web service, then the PHP script will push the html pages

You can use the tag base to solve the problem of the relative path of the files, something like this:

$html = file_get_contents($url);
$html = str_replace('<head>', '<head><base href="FULL PATH OF DIR" />', $html);
echo $html;

CSS and images are not displayed because their paths in the HTML files is relative to HTML files, right? And if you have these CSS and images in the same private folder, how can you hope the user will fetch them?

Indirect, you should fetch CSS and images the same way you do with HTML. But this means you have to replace all paths in your displayed HTML, that is quite absurd. In fact, we are talking about some kind of proxy now... ?!?!?

Why you need it?

Anyway echo(file_get_contents($htmlFile)); is less stressful.

Another option if it is an <img /> tag and the image is also stored outside of the root you can just make the src= attribute as so:

src="get_image.php?file=thisfile.png" // add a $_GET if needed to distinguish files

then get_image.php:

  $file = $_GET['file'];

  // security checks if you wish

  header(sprintf("Content-type: %s;",'image/png'));
  readfile($file);
  exit;