Answer the question
In order to leave comments, you need to log in
1 folder and 10,000 pictures or 10,000 folders and one picture each?
The site is being developed. In which case will the performance be higher - put each picture in a separate folder (by id) or create one folder and put all the pictures there?
Answer the question
In order to leave comments, you need to log in
a good practice that is used in many places, for example, Microsoft
calculates md5 for a file - 2274facdbca56499fe397344c633e25a
put the file in the avatars/227/4fa/cdb/2274facdbca56499fe397344c633e25a.jpg folder
Depends on how access will be provided. If only reading by url without enumeration - you can put it in one folder, but such cases are rare, go to the folder via ftp and you will be upset by the read error message.
If it is possible to find out, for example, the date of uploading / adding a picture, then it’s better to put it in folders, for example / year / month /
10,000 pictures will definitely not accumulate in a month))
In any case, it’s more convenient to keep a small number of pictures in separate folders .
for an avatar or what?)
I think it’s better to collect the whole bunch in one folder, because access to the picture goes through a direct url and it doesn’t matter how many pictures are in the folder, and if there is a search through the files, then of course the second option is better.
and you can use the second option (a bunch of folders, one photo), so that in the future it would be possible to put more than one photo in this folder.
it all depends on the case.
If you directly answer the question, then I think things are like this (if I'm wrong, correct me):
1) If you have the path to the image stored in the database, then it doesn't matter in terms of performance, it's users/user_10000/avatar.png or users/avatar_10000 .png
2) Each created directory is an overhead (on most file systems, about 4 KB, as I understand it)
3) Fragmentation does not play a role here, since the server is usually on Unix, so it's okay (I'm not sure here)
Better - the golden mean: 100 folders with 100 pictures in each.
The folder is read the same file. If there are too many files in the directory, the brakes begin. Therefore, it is better to split everything so that the files are in more or less unique folders, but at the same time there are not many of these folders. For example, as suggested above, add folders with the name of the date (moreover, it should be 3 levels of folders - for the year, for the month and for the date).
I propose to take a ready-made class from here
bablogon.net/view.php?p=151 pass
the class in the class folder to the
class id and the class forms a tree structure, you can also specify the number of files in the folder and the number of branches
to get the address for the file is elementary, you only need to know its id
with folders is the same
<?php
/**
* MVCPHP Framework
*
* @link http://mvcphp.ru/
*
*/
/**
* Класс загрузки
*
*/
class Upload {
public $id;
private $upload_dir;
private $max_file_count;
private $branches;
public function __construct(array $param=array()) {
$def_param=array('upload_dir'=>Q_PATH.'/uploads/','max_file_count'=>1000,'branches'=>2,'pattern'=>'');
$upload_param=Functions::arr_union($def_param,$param);
$this->upload_dir=$upload_param['upload_dir'];
$this->max_file_count=$upload_param['max_file_count'];
$this->branches=$upload_param['branches'];
//сложность надумана, все зависит от инодов df -i и tune2fs -l /dev/hda1 и df -Ti
switch($upload_param['pattern']) {
case 'bigint':
$this->max_file_count=512;
$this->branches=6;
break;
case 'int':
$this->max_file_count=216;
$this->branches=3;
break;
case 'mediumint':
$this->max_file_count=204;
$this->branches=2;
break;
case 'smallint':
$this->max_file_count=182;
$this->branches=1;
break;
}
$this->del_id();
}
public function set_id($id) {
$this->id=$id;
}
public function del_id() {
$this->id=0;
}
public function find_upload($url) {
if(is_file($url)) {
return true;
}
else {
return false;
}
}
public function get_upload($id,$fl) {
$this->set_id($id);
for($i=$this->branches;$i>=1;$i--) {
$dir=ceil($this->id/pow($this->max_file_count,$i))%$this->max_file_count;
$dir_file_arr[]=$dir>0?$dir:$this->max_file_count;
}
$dir_file_str=implode("/", $dir_file_arr);
return $this->upload_dir.$dir_file_str.'/'.$this->id.$fl;
}
public function put_upload($id,$fl,$data) {
$this->set_id($id);
for($i=$this->branches;$i>=1;$i--) {
$dir=ceil($this->id/pow($this->max_file_count,$i))%$this->max_file_count;
$dir_file_arr[]=$dir>0?$dir:$this->max_file_count;
$dir_file_str=implode("/", $dir_file_arr);
if(!is_dir($this->upload_dir.$dir_file_str)) {
mkdir($this->upload_dir.$dir_file_str, 0777);
chmod($this->upload_dir.$dir_file_str, 0777);
}
}
file_put_contents($this->upload_dir.$dir_file_str.'/'.$this->id.$fl, $data);
return $this->upload_dir.$dir_file_str.'/'.$this->id.$fl;
}
public function set_upload($id,$fl) {
$this->set_id($id);
for($i=$this->branches;$i>=1;$i--) {
$dir=ceil($this->id/pow($this->max_file_count,$i))%$this->max_file_count;
$dir_file_arr[]=$dir>0?$dir:$this->max_file_count;
$dir_file_str=implode("/", $dir_file_arr);
if(!is_dir($this->upload_dir.$dir_file_str)) {
mkdir($this->upload_dir.$dir_file_str, 0777);
chmod($this->upload_dir.$dir_file_str, 0777);
}
}
return $this->upload_dir.$dir_file_str.'/'.$this->id.$fl;
}
public function get_upload_dir($id) {
$this->set_id($id);
for($i=$this->branches;$i>=1;$i--) {
$dir=ceil($this->id/pow($this->max_file_count,$i))%$this->max_file_count;
$dir_file_arr[]=$dir>0?$dir:$this->max_file_count;
}
$dir_file_str=implode("/", $dir_file_arr);
return $this->upload_dir.$dir_file_str.'/'.$this->id;
}
public function set_upload_dir($id) {
$this->set_id($id);
for($i=$this->branches;$i>=1;$i--) {
$dir=ceil($this->id/pow($this->max_file_count,$i))%$this->max_file_count;
$dir_file_arr[]=$dir>0?$dir:$this->max_file_count;
$dir_file_str=implode("/", $dir_file_arr);
if(!is_dir($this->upload_dir.$dir_file_str)) {
mkdir($this->upload_dir.$dir_file_str, 0777);
chmod($this->upload_dir.$dir_file_str, 0777);
}
}
if(!is_dir($this->upload_dir.$dir_file_str.'/'.$this->id)) {
mkdir($this->upload_dir.$dir_file_str.'/'.$this->id, 0777);
chmod($this->upload_dir.$dir_file_str.'/'.$this->id, 0777);
}
return $this->upload_dir.$dir_file_str.'/'.$this->id;
}
}
Didn't find what you were looking for?
Ask your questionAsk a Question
731 491 924 answers to any question