Hide real path of images in php is possible?

前端 未结 2 1670
清歌不尽
清歌不尽 2020-12-20 10:23

In my web server, I have these files. I don\'t want user to find out my real path to \"p1.jpg\" and \"p2.jpg\".

相关标签:
2条回答
  • 2020-12-20 10:38

    Use Apache Rewrites.

    I'm not that experienced with Apache Rewrites but you might create a .htaccess file in that directory which contains a line something like this:

    RewriteRule   ^/photos/(.+).jpg$  /images/$1   [R]
    
    0 讨论(0)
  • 2020-12-20 10:50

    So as I see it, there are two parts to this.

    1) Make it so the contents of images/ is hidden

    2) Make it so a person can access photos/somefile.jpg and have the server serve images/somefile.jpg

    1) Hide contents of images/

    Create a .htaccess file in images/ and put the following in it

    Order allow,deny
    Deny from all
    

    2) Serving the file

    Create a .htaccess file in the photos/ and put the following in it

    RewriteEngine On
    RewriteRule ^([a-zA-Z0-9_.-]+).jpg$ photo.php?image_name=$1
    

    And create the php file photo.php in the photos/ directory

    <?php
    
    if(isset($_GET['image_name'])){
    
      if(file_exists("../images/" . $_GET['image_name'].".jpg")){
    
          header('Content-Type: image/jpg');
          readfile("../images/" . $_GET['image_name'].".jpg");
      }
    
    }
    

    I'm doing that blind, but will test in a sec!

    0 讨论(0)
提交回复
热议问题