代码之家  ›  专栏  ›  技术社区  ›  Richard Knop

Serving images from outside of document root

  •  8
  • Richard Knop  · 技术社区  · 14 年前

    有可能吗?

    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.

    2 回复  |  直到 10 年前
        1
  •  12
  •   unbeli    14 年前

    The most common way is an Alias :

    Alias /data/ /wherever/your/data/are/
    
        2
  •  5
  •   tweetlogist    10 年前

    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.