Hide real path of images in php is possible?

為{幸葍}努か 提交于 2020-01-14 05:33:07

问题


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".

When i show "p1.jpg" in "index.php", how to hide original path "/images/p1jpg" to something like this "/photo/p1.jpg". Is it possible?


回答1:


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!




回答2:


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]


来源:https://stackoverflow.com/questions/31471707/hide-real-path-of-images-in-php-is-possible

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