Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I block direct access to images in a directory but allow PHP to display them?

Tags:

php

.htaccess

I'm using a .htaccess file with the following code:

Order deny, allow
Deny from all
Allow from localhost

But when I display the images I'm just putting a link to the image in an tag, but the images are not showing up.

How can I display the image but disallow direct access to it? Is there a way for me to just copy the raw bytes of the file and display them?

like image 358
user2720360 Avatar asked Dec 10 '25 21:12

user2720360


1 Answers

Be sure to save embedded.png in same folder as this example source.

<?php
function data_uri($file, $mime) 
{  
  $contents = file_get_contents($file);
  $base64   = base64_encode($contents); 
  return ('data:' . $mime . ';base64,' . $base64);
}
?>

<html>
    <h1>Embedded Image:-</h1>
    <img src="<?php echo data_uri('embedded.png','image/png'); ?>" alt="Embedded image" />
</html>
like image 190
Jimmy Avatar answered Dec 13 '25 10:12

Jimmy



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!