Картинки пользователя над корнем сайта - PullRequest
1 голос
/ 09 февраля 2012

Я делаю базу данных управления членом. Я хочу разместить все изображения участников над корнем сайта, чтобы к ним нельзя было получить доступ через URL, но я хочу показать изображение участника в профиле участника. Я использую php. Как мне это сделать?! Спасибо!

1 Ответ

1 голос
/ 09 февраля 2012

Вы можете создать 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();
    }
}
...