Thusitha Wickramasinghe
Thusitha Wickramasinghe

Reputation: 1103

Hide real path of images in php is possible?

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

file on server

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?

Upvotes: 0

Views: 7437

Answers (2)

CT14.IT
CT14.IT

Reputation: 1737

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!

Upvotes: 5

EM-Creations
EM-Creations

Reputation: 4301

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]

Upvotes: 3

Related Questions