-1

I want to display images, from files, in an HTML webpage, using PHP to hide the location of the files, using PHP header/readfile, and avoiding any PHP that would reveal the file location (such as echo).

So far I've only been able to get this working by calling a PHP scrpit from the HTML, but would prefer to do so without calling a script, so a viewer has no sight of the PHP script file. I do not want to use a PHP file rather than HTML file (so a viewer couldn't type the URL of the PHP script in themselves).

image.php:

<?php
header('Content-Type:image/jpeg'); 
readfile('../image.jpg');
?>

image.html:

<html>
...
<img src="image.php"/>

In the HTML file I would like to do the equivalent but in-line:

<html>
...
<img src="<?php header('Content-Type:image/jpeg');readfile('../image.jpg');?>"/>

Or:

<html>
...
<?php
echo '<img src="';
header('Content-Type:image/jpeg'); 
readfile('../image.jpg');
echo '"/>';
?>

I suspect my lack of understanding of how HTML and PHP work together is letting me down here.

mikeysoft
  • 1
  • 1

3 Answers3

1

I would like to do the equivalent but in-line:

You cannot. You must have separate request. You could optionally inline the image as base64 but that bad idea anyway.

Also this code looks like pointless - you just exposing existing file w/o any benefit of doing that yourself BUT with the penalty of killing all the caching and other features browsers could do. Unless you know how to do that properly (this is not that trivial), you are complicating simple thing instead of simplifying complicated.

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • I'm not interested in caching. This is a personal website with very low usage (by me only). All I'm trying to do is protect the paths of images, so it's not "pointless" (thanks very much for that comment). In the real HTML page I've created, rather than this simplified example, a number of images are displayed together on a single page, so I'm not complicating anything. – mikeysoft May 11 '19 at 16:23
  • 1
    Well, if you are not open to constructive criticism you may find this place unfriendly. And protecting or not you are killing browser cache, causing every request to fetch the data that could otherwise be cached by client not to mention `HEAD` requests etc. That's why it is considered bad unless implemented properly. Nonetheless, if you do not care, then answer is same: you can inline just base64 form which would add additional 20% to the whole image size. mod rewrite based solution would would only make image urls prettier. Still I'd say it's rather bad idea for bigger items than icons etc. – Marcin Orlowski May 11 '19 at 18:46
  • "this code looks like pointless" - negative, not constructive. caching - the images are CCTV images and change every load, so no "killing browser cache" - please don't make assumptions. – mikeysoft May 14 '19 at 10:12
1

What you want can not be done as you're trying. You can echo the contents via the base64 aproach. but that would make your html grow in size very rapidly, which isnt good for performance.

There is a way you can get it to work though. It's a bit trickier, but you can use your .htaccess file for this. Normally you often use it to rewrite some url to redirect the url to the index.php. You can also use it to create an image url:

RewriteEngine On
RewriteRule ^/special-images/(.*)\.jpg$ /php-file-directory/image.php?image-name=$1 [L]

If you now do <img src="/special-images/bob.jpg" /> it will internally open-image.php with $_GET['image-name'] being bob.

*Cant test the htaccess right now, but you get the gist of it.

Martijn
  • 15,791
  • 4
  • 36
  • 68
-1

try coverting it to a data url

https://stackoverflow.com/a/13758760/11485791

example:

<img src="<?
php path = '../image.jpg';
$type = pathinfo($path, PATHINFO_EXTENSION);
$data = file_get_contents($path);
$base64 = 'data:image/' . $type . ';base64,' . base64_encode($data);
echo $base64;
?>"/>

but then the returned html would be huge

MaxSilvester
  • 193
  • 1
  • 12