我怎样才能阻止直接访问图像目录但允许PHP来显示它们? [英] How can I block direct access to images in a directory but allow PHP to display them?

查看:130
本文介绍了我怎样才能阻止直接访问图像目录但允许PHP来显示它们?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我用用下面的code .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?

推荐答案

一定要保存的 embedded.png 的在同一个文件夹如本例源。

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>

这篇关于我怎样才能阻止直接访问图像目录但允许PHP来显示它们?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆