douyingmou1389 2013-02-02 21:05
浏览 5

如何在网站中存储和管理用户的图像

I'm creating a dating site where users could upload their images, some private and some public.

What I planned to do is to save all the uploaded images, and when I would like to show them I will have a script that will call them.

ie: url-> mysite.com/members/mainimage/5

the function that handles this call is: public function mainImage($user) {

    $this->load->model('tables/user_images_table');
    $image = $this->user_images_table->getMainUserImage($user);

    if($image != null ) {
        $imageExploded = explode('.',$image);

        switch( $imageExploded[1] ) {
            case "gif": $ctype="image/gif"; break;
            case "png": $ctype="image/png"; break;
            case "jpeg":
            case "jpg": $ctype="image/jpg"; break;
            default:
        }
        $imagePath = 'uploads/'.$image;
    } else {
        //default image
        $imagePath = 'inner/default.png';
        $ctype = 'image/png';
    }



    header('Content-type: '.$ctype);
    readfile(base_url()."images/".$imagePath);
}

but after I see my site is getting bigger, I notice that each user image request all my appliaction files are loaded, and it probably will slow my site down.

do you have any idea how to save private images (that wont will be public by url, only after some checks on the user)

  • 写回答

1条回答 默认 最新

  • douxiaomang5640 2013-02-02 21:31
    关注

    store your private images in some folder, also place there .htaccess with such code:

    deny from all
    

    so now no one can access these files via http directly

    then create .htaccess in the root of your site and copy these lines there:

    RewriteEngine On
    RewriteCond %{REQUEST_FILENAME} !-d
    RewriteCond %{REQUEST_FILENAME} !-f
    RewriteRule ^private-photo/([0-9]+)\.(jpg|jpeg|gif|png)$ private_photo.php?id=$1 [QSA,L]
    

    so now all http requests like http://site.com/private-photo/113.jpg will be redirected to private_photo.php

    so the final step is: you create private_photo.php where you get id of image from $_GET["id"], make security checkings - if the user who requests the photo has an access to it, and then fopen/fpassthru the image or just displaying an error about restricted access

    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题