1

我有一个上传图像的 php 脚本,将其重命名为随机散列,根据散列创建文件夹,将原始图像存储在最后创建的文件夹中,最后为它创建 2 个其他缩略图...

常规图像...images/a3c/65c/297/108_a3c65c2974270fd093ee8a9bf8ae7d0b.jpg

缩略图...images/a3c/65c/297/108_a3c65c2974270fd093ee8a9bf8ae7d0b_t.jpg

较小的缩略图...images/a3c/65c/297/108_a3c65c2974270fd093ee8a9bf8ae7d0b_sm.jpg

对于上传的每张照片,它将在数据库中存储一条新记录,其中包含照片 ID 和此信息

/a3c/65c/297/108_a3c65c2974270fd093ee8a9bf8ae7d0b

(注意我没有存储“.jpg”或“_t.jpg”等)

现在为了让我提供图像(我不知道这是否是一个好习惯),目前我有这个返回图像的 php 函数......

function get_image($image_id,$type = '')
{
    $sql = "SELECT * FROM photos where photo_id = {$image_id}";
    $query = $this->db->query($sql);
    $r = $query->row();

    $image = base_url().'images/'.$r->dir_1 . '/' . $r->dir_2 . '/' . $r->dir_3 . '/'.$image_id.'_'.$r->img_hash;

    if($type == 'small')
        return $image.'_sm.jpg';
    if($type == 'reg_thumb')
        return $image.'_t.jpg';
    if($type == '' || $type == 'original')
        return $image.'.jpg';

}

基本上,我想做一些像推特这样的图片网址看起来像这样的事情

https://twimg0-a.akamaihd.net/profile_images/2392722244/blah.jpg

问题...

  1. 我怎样才能实现这种类型的 url 结构?访问?
  2. 我是否以正确的方式返回图像?
  3. 我在做什么是安全、聪明和好的做法吗?如果没有,有更好的方法吗?

谢谢!!

4

1 回答 1

1

IMO,您确实可以通过 url 重写来做到这一点。在 /etc/apache2/sites-available/yoursite ,

    <Directory />

            RewriteEngine On
            RewriteCond %{HTTP_REFERER}          profile_images/(\w+)/(\w+|.)+$


            RewriteRule ^/profile_images/(\w+)/         index.php?image_id=$1 [L,QSA]

            Options Indexes FollowSymLinks MultiViews
            AllowOverride None

            Order allow,deny
            allow from all
    </Directory>

尝试解决这个问题。我认为它与 htaccess 中的指令非常相似。

Am I returning the images the right way?

你是什​​么意思 ?你的“回报”会起作用。我会使用 switch/case 语句而不是 ifs。

is what im doing safe, smart, and a good practice? if not is there a better method?

安全:你应该逃避你的输入。但是,如果您仅重定向图像名称为字母数字的图像,则应该不是问题

我不知道你为什么分解成这么多目录。每个图像+缩略图一个目录就足够了。不要担心使用 b-tree 或类似的东西进行索引,仅在您的数据库中使用索引就足够了。

最后但同样重要的是,如果我不需要在用户和图像之间建立链接,我只会使用 apache 的重写规则来实现这一点。

RewriteRule ^/profile_images/(\w+)(_\w+)?.(jpg|png)/images/$1/$1$2.$3

它不涉及 php 也不涉及 db,并且是在一行中完成的。

https://serverfault.com/questions/214512/everything-you-ever-wanted-to-know-about-mod-rewrite-rules-but-were-afraid-to-as

http://httpd.apache.org/docs/current/mod/mod_rewrite.html

我不确定这是否有帮助,但你可能已经学到了一些东西。至少,你有另一个观点:)

于 2012-09-17T13:23:46.827 回答