Monday, January 21, 2013

hide image path in a PHP

 

            Image protecting in a web site is the major security issue. But we cannot  make it 100% secure, atleast we can make it difficult. In PHP  we can make  the image  path  hidden, customer/web user cannot access the image folder or  image path.

    Here are the steps to hide the direct URL/real path  of the image.

Copy the bellow code and put it in  the  new php file  image.php.

image.php

<?php

$id = $_GET['id'];


$map1 = {

1=>'c:/picture/jellyfish.jpg',
2=>'c:/picture/cfish.jpg',
3=>'c:/picture/dfish.jpg'
        }

$path = $map1[$id];

$str = file_get_contents($path);
echo $str; 

?>


and  in the html  page(suppose test.html), use  the code  like this bellow

<img src='image.php?id=3'>

-------------------------------------------------------------------------------------------------------------------------------------------------
if you are using database,  get  the  path of the  image from database  and put it in the array.

<?php 


$root = $_SERVER['DOCUMENT_ROOT'];

$db = new db_mysql();
$id = $_GET['id'];
$sql = "SELECT id, org_id, div_id, file_name FROM photos WHERE id='".$id."'";
$map1 = Array();
$db->query($sql);
while ($db->next_record())
{
    $id = $db->f('id');
    $map1[$id]['name'] = $root."/photos/".$db->f('org_id')."/".$db->f('div_id')."/".$db->f('div_id')."_".$db->f('file_name');


$path = $map1[$id]['name'];

$str = file_get_contents($path);
echo $str;  

?>


if you have any query, please  feel  free to comment.

4 comments:

  1. This comment has been removed by the author.

    ReplyDelete
  2. To hide image / file path or source you can try http://pvtlink.com

    ReplyDelete
  3. Hi Sharath, I've been searching a method to hide image url from direct access, thanks for sharing this code.

    As I don't know PHP, and my pages are written in html, may I ask, to apply this code, should I put the image.php in ''image'' folder? and for 1=>'c:/picture/jellyfish.jpg' , is it correct to write e.g. 1=>'/img folder/image.jpg' , should I place any PHP code in of html file? Thank you very much for helping in advance.

    ReplyDelete