【发布时间】:2012-02-09 17:19:29
【问题描述】:
我正在制作会员的管理数据库。我想将所有成员图片放在站点根目录上方,以便无法通过 url 访问它们,但我想在成员个人资料中显示成员的图片。我正在使用 php。我怎样才能做到这一点?!谢谢!
【问题讨论】:
我正在制作会员的管理数据库。我想将所有成员图片放在站点根目录上方,以便无法通过 url 访问它们,但我想在成员个人资料中显示成员的图片。我正在使用 php。我怎样才能做到这一点?!谢谢!
【问题讨论】:
你可以创建一个php文件,根据一些输入参数读取文件,同时根据一些条件决定是否允许访问者查看文件。
类似这样的东西(简化示例):
<?php
// presuming this file is in the root of your site
// define some directory where the actual images are
$dir = realpath( dirname( __FILE__ ) . '/../profile-images' );
// presuming this file is called with something like
// image.php?profileImage=fireeyedboy.jpg
if( isset( $_GET[ 'profileImage' ] ) )
{
// strip all possible redundant paths
// you should probably sanitize even more (check valid extensions etc.)
$profileImage = basename( $_GET[ 'profileImage' ] );
if( $someConditionsThatVisitorIsAllowedToViewThisImageAreMet )
{
// presuming mime type jpeg for now
header( 'Content-Type: image/jpeg' );
readfile( $dir . '/' . $profileImage );
exit();
}
else
{
// conditions are not met, dish out HTTP/1.1 403 Forbidden header
header( 'HTTP/1.1 403 Forbidden', true );
exit();
}
}
【讨论】: