Reputation: 898
Idea is to store user profile photos to file server. I have done with uploading part to static folder, but I want to make It a bit dynamic.
File name is generating in following: $userid . '-' . round(microtime(true)) . '.jpg';
I want to store images based on $userid
, 1000 per folder.
So it have to check if $userid <= 1000
and folder not exists, create new folder named 000001-100000
.
For example if $userid = 1001
it have to check if folder not exists and create new folder with name 002001-002000
and so on.
How could I achieve it dynamically? There could be over 100 000 users, so checking in following not so best idea I think:
if (!file_exists('images/000001-001000')) && $userid <= 1000 {
mkdir('images/000001-001000', 0777, true);
}
if (!file_exists('images/001001-002000')) && $userid > 1000 && $userid <= 2000 {
mkdir('images/001001-002000', 0777, true);
}
p.s. this is not duplicated as marked, I need to store 1000 photos per folder, not to create specific folder for each user.
Upvotes: 1
Views: 159
Reputation: 1707
Just generate folders once. If you can't predict which folder is last, you may generate 5-50 folder more and check it with cron; do I need to generate more folders or not?
Upvotes: 0
Reputation: 16436
Try this I have created recursive method to check value. Then create directory according it
function get_thousand_value($userid,$value_min)
{
if($userid <= $value_min){
$return_val = $value_min;
}
else
{
$value_min += 1000;
$return_val =get_thousand_value($userid,$value_min);
}
return $return_val;
}
$value_min = 1000;
$last_val = get_thousand_value($userid,$value_min);
$start_val = ($last_val == 1000 ? '0001' : ($last_val - 999));
$dir_name= 'images/'.$start_val."-".$last_val;
if (!file_exists($dir_name)) {
mkdir($dir_name, 0777, true);
}
Upvotes: 0
Reputation: 21
$count = floor($userid / 1000);
$begin = ($count * 1000) + 1;
$end = ($count + 1) * 1000;
$strBegin = $begin;
$strEnd = $end;
if($begin==1){
$strBegin = "0001";
}
if(is_dir('images/'.$strBegin.'-'.$strEnd)==false){
mkdir('images/'.$strBegin.'-'.$strEnd);
}
Upvotes: 2