How to hide the physical path of the images

2019-02-27 16:56发布

In my application i display the image of user by giving the physical path of the directories like http://www.example.com/user_images/abcdefghijk.jpg

But i dont want to expose this physical path to the external users to access the direct files. I want to implement the image retrieval as per the gravtar type image display. How we can do that?

2条回答
放我归山
2楼-- · 2019-02-27 17:15

in php you can save a variable in session and append it to image's path like

<img src="php_function.php?token=1234" />

and in php file get the image reading from session its actual path and return the content by file_get_contents

here is the source http://hashmode.com/hide-image-path-php/9

查看更多
孤傲高冷的网名
3楼-- · 2019-02-27 17:23

In PHP you can use GD library to read the image. So the only way you need to create a image.php file & use the following code,

<?php
$imagepath="your image physical path";

$image=imagecreatefromjpeg($imagepath);

header('Content-Type: image/jpeg');
imagejpeg($image);
?>

this is for jpg image for all other images you can use different functions fo GD library.

So Gravtar.com uses the above way to display the image :)

In ROR you can use Rmagick gem to read the image & display the image with the same way.

Thanks

查看更多
登录 后发表回答