Thousands of images, how should I organize the directory structure? (linux)

前端 未结 6 1082
一向
一向 2020-12-07 23:31

I am getting thousands of pictures uploaded by thousands of users on my Linux server, which is hosted by 1and1.com (I believe they use CentOS, but am unsure of the version).

6条回答
  •  一生所求
    2020-12-08 00:18

    Here are two functions I wrote a while back for exactly this situation. They've been in use for over a year on a site with thousands of members, each of which has lots of files.

    In essence, the idea is to use the last digits of each member's unique database ID to calculate a directory structure, with a unique directory for everyone. Using the last digits, rather than the first, ensures a more even spread of directories. A separate directory for each member means maintenance tasks are a lot simpler, plus you can see where's people's stuff is (as in visually).

    // checks for member-directories & creates them if required
    function member_dirs($user_id) {
    
        $user_id = sanitize_var($user_id);
    
        $last_pos = strlen($user_id);
        $dir_1_pos = $last_pos - 1;
        $dir_2_pos = $last_pos - 2;
        $dir_3_pos = $last_pos - 3;
    
        $dir_1 = substr($user_id, $dir_1_pos, $last_pos);
        $dir_2 = substr($user_id, $dir_2_pos, $last_pos);
        $dir_3 = substr($user_id, $dir_3_pos, $last_pos);
    
        $user_dir[0] = $GLOBALS['site_path'] . "files/members/" . $dir_1 . "/";
        $user_dir[1] = $user_dir[0] . $dir_2 . "/";
        $user_dir[2] = $user_dir[1] . $dir_3 . "/";
        $user_dir[3] = $user_dir[2] . $user_id . "/";
        $user_dir[4] = $user_dir[3] . "sml/";
        $user_dir[5] = $user_dir[3] . "lrg/";
    
        foreach ($user_dir as $this_dir) {
            if (!is_dir($this_dir)) { // directory doesn't exist
                if (!mkdir($this_dir, 0777)) { // attempt to make it with read, write, execute permissions
                    return false; // bug out if it can't be created
                }
            }
        }
    
        // if we've got to here all directories exist or have been created so all good
        return true;
    
    }
    
    // accompanying function to above
    function make_path_from_id($user_id) {
    
        $user_id = sanitize_var($user_id);
    
        $last_pos = strlen($user_id);
        $dir_1_pos = $last_pos - 1;
        $dir_2_pos = $last_pos - 2;
        $dir_3_pos = $last_pos - 3;
    
        $dir_1 = substr($user_id, $dir_1_pos, $last_pos);
        $dir_2 = substr($user_id, $dir_2_pos, $last_pos);
        $dir_3 = substr($user_id, $dir_3_pos, $last_pos);
    
        $user_path = "files/members/" . $dir_1 . "/" . $dir_2 . "/" . $dir_3 . "/" . $user_id . "/";
        return $user_path;
    
    }
    

    sanitize_var() is a supporting function for scrubbing input & ensuring it's numeric, $GLOBALS['site_path'] is the absolute path for the server. Hopefully, they'll be self-explanatory otherwise.

提交回复
热议问题