Serving images from outside of document root

时光毁灭记忆、已成空白 提交于 2020-01-10 03:44:08

问题


Is it possible?

Let's say my directory structure looks like this:

/data
    /data/images
/public

The document root is in the "public" directory. This won't work, of course:

<img src="/../data/images/1.png" alt="" />

But maybe it's possible to enable serving images from directory above document root somehow? I would like to know.


回答1:


The most common way is an Alias:

Alias /data/ /wherever/your/data/are/



回答2:


Another way (say if you can't set an alias...) would be to use a script as the image source eg:

<img src="image.php?img=myImage.jpg" />

image.php would be similar to:

<?php
  header('Content-Type: image/jpg');
  readfile("../img/" . $_GET['img']);
?>

Of course you'd need to extend the script to filter $_GET['img'], as well as detect and set the right content type. Your script could even resize the image according to other parameters passed in the query string.



来源:https://stackoverflow.com/questions/3131562/serving-images-from-outside-of-document-root

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!