SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Member
    Join Date
    Oct 2008
    Posts
    7
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Unhappy Access Prevent Image Directory

    Hello every one,

    I prevent my image directory by using .htaccess file by :

    Code:
     Options -Indexes
    but when I tried to display image from this directory it's not display

    Code:
    echo '<img src="img/?????.jpg" />';
    My question is .. how prevent directory an files inside it and at the same time I could display image .

    Thanks

  2. #2
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,481
    Mentioned
    164 Post(s)
    Tagged
    1 Thread(s)
    The "Options -Indexes" directive stops Apache from showing the default view of the folder's contents when it does not contain an "index" page. eg. where an HTTP request for www.yoursite.com/img/" would show something like
    Code:
    ..
    imgA.jpg
    logo.jpg
    menu.png
    This does not stop someone from accessing the folder's contents once they know the details. eg. I could hotlink to your image using
    HTML Code:
    <img src="http://www.yoursite.com/img/imgA.jpg" />
    and your image would show in my page.

    If you don't want anyone to be able to do that, you can put your image folder outside of your site's root folder (i.e. next to your "public html" folder) and access from your pages by "backing up" to it. eg. in your main index page located at www.yoursite.com/index.html you would use
    HTML Code:
    <img src="../img/imgA.jpg" />

  3. #3
    Follow Me On Twitter: @djg gold trophysilver trophybronze trophy Dan Grossman's Avatar
    Join Date
    Aug 2000
    Location
    Philadephia, PA
    Posts
    20,580
    Mentioned
    1 Post(s)
    Tagged
    0 Thread(s)
    Quote Originally Posted by Mittineague View Post
    If you don't want anyone to be able to do that, you can put your image folder outside of your site's root folder (i.e. next to your "public html" folder) and access from your pages by "backing up" to it. eg. in your main index page located at www.yoursite.com/index.html you would use
    HTML Code:
    <img src="../img/imgA.jpg" />
    Ehrm, that doesn't work. The browser can't make an HTTP request for a URI below the document root.

  4. #4
    Programming Team silver trophybronze trophy
    Mittineague's Avatar
    Join Date
    Jul 2005
    Location
    West Springfield, Massachusetts
    Posts
    16,481
    Mentioned
    164 Post(s)
    Tagged
    1 Thread(s)
    :d'oh: Of course you're right about that. I must have been thinking about include -- or not thinking at all

    So it's more complicated but not impossible.
    You could do something like
    <img src="./inc/imgA.php" />
    where imgA.php fopens
    ../../img/imgA.jpg
    and serves it with the right MIME type headers.

    If you wanted to get fancy you could use Apache mod_rewrite to change requests like img/imgA.jpg into inc/img.php?image=imgA.jpg

    Another benefit of having a PHP file get the image is you can test for SESSION cookie, etc if you want to to keep people from just hotlinking to the PHP file.


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •