有了這個解決方案總是會有一個最大的在每個目錄
隨着文件(編號的)範圍從1到1999年的10個文件(編號的)在根目錄將是這樣的:
一個ID爲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';
}
創建路徑
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');
}
GET路徑210
類
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;
}
}
或許也可以參考答案:如何將圖像存儲在文件系統(http://stackoverflow.com/questions/191845/how-to-store-images-in-your文件系統) – Jacco