与数据库链接的文件的文件结构

时间:2011-11-08 08:16:25

标签: php mysql database

如何存储与数据库链接的文件的最佳方式,其中没有太多文件存储在同一目录中(减慢FTP浏览速度等)

我自己的建议:

3000000/0/70000/9000/100/30/3079138.full.pdf
3000000/0/70000/9000/100/30/3079138.thumb.png
3000000/0/70000/8000/900/20/3078928.full.png
3000000/0/70000/8000/900/20/3078928.thumb.pdf
3000000/0/70000/8000/900/20/3078929.full.pdf
3000000/0/70000/8000/900/20/3078929.thumb.png

2 个答案:

答案 0 :(得分:1)

使用此解决方案,每个目录中最多总共有10个文件(id)

对于从1到1999的文件(id),根目录中的目录将如下所示: enter image description here

创建ID

的路径
require_once 'File_structure.php';    
for($i=1; $i<2000; $i++){
    $File = new File_structure();
    $File->id = $i;
    $File->prepend_path = 'image/';
    $path = $File->create();
    file_put_contents($path.$i, 'sd');
}

获取ID

的路径
require_once 'File_structure.php';
$id = 1254;
$File = new File_structure();
$File->id = $id;
$File->prepend_path = 'image/';
$path = $File->get();
if(is_file($path.$id)){
    echo 'ok';
}

class File_structure {
    public $id;
    public $prepend_path = '';

    private $path = array();

    function get(){
        $this->path();

        return $this->prepend_path.(substr($this->prepend_path, -1) == '/' ? '':'/').implode('/', $this->path).'/';
    }

    function create(){
        $this->path();

        $path = substr($this->prepend_path, -1) == '/' ? substr($this->prepend_path, 0, -1):$this->prepend_path;
        foreach($this->path as $dir){
            $path .= '/'.$dir;
            if(!is_dir($path)){
                mkdir($path);
                chmod($path, 0777);
            }
        }

        return $path.'/';
    }

    function path(){
        $this->prepare_id();

        $length = strlen($this->id);
        for($i=0; $i<$length; $i++){
            $len = $length - $i;
            if($len <= 1){
                break;
            }
            if($path = (int)str_pad($this->id[$i], $len, 0, STR_PAD_RIGHT)){
                $this->path[] = $path;
            }
        }
    }

    function prepare_id(){
        $this->id = (string)$this->id;
    }
}

答案 1 :(得分:0)

通常我会根据易于使用的简单模式存储文件:

uploads/<year>/<month>/<day>/full.pdf
uploads/<year>/<month>/<day>/thumb.png

我自然检查文件名冲突并纠正它们