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.