I want to create new folder for images dynamically, when one directory gets 1000 images in there Using PHP, MySQL what is best practice to achieve this kind of thing? :) Thanks
Asked
Active
Viewed 2,111 times
0
-
1`mkdir` => http://php.net/manual/ru/function.mkdir.php – W.D. Oct 28 '14 at 11:03
-
yeah, i know about mkdir function, but i'm asking about different thing. how to check that in folder allready is 1000 images and then create new folder dynamically for one more 1000 images and so on. – Dimi Mikadze Oct 28 '14 at 11:11
5 Answers
0
To count the number of files within a folder, I refer you to this answer.
You would then use the mkdir() function to create a new directory.
So you would have something like:
$directory = 'images';
$files = glob($directory . '*.jpg');
if ( $files !== false )
{
$filecount = count( $files );
if ($filecount >= 1000)
{
mkdir('images_2');
}
}

Community
- 1
- 1

danhardman
- 633
- 1
- 6
- 16
0
From this example Count how many files in directory php
Add an if statement that will create a folder when files reach a certain number
<?php
$dir = opendir('uploads/'); # This is the directory it will count from
$i = 0; # Integer starts at 0 before counting
# While false is not equal to the filedirectory
while (false !== ($file = readdir($dir))) {
if (!in_array($file, array('.', '..') and !is_dir($file)) $i++;
if($i == 1000) mkdir('another_folder');
}
echo "There were $i files"; # Prints out how many were in the directory
?>

Community
- 1
- 1

Monwabisi Sifumba
- 40
- 10
0
you can try something like this
$dir = "my_img_folder/";
if(is_dir($dir)) {
$images = glob("$dir{*.gif,*.jpg,*.JPG,*.png}", GLOB_BRACE); //you can add .gif or other extension as well
if(count($images) == 1000){
mkdir("/path/to/my/dir", 0777); //make the permission as per your requirement
}
}

Ram Sharma
- 8,676
- 7
- 43
- 56
0
define("IMAGE_ROOT","/images");
function getLastFolderID(){
$directory = array_diff( scandir( IMAGE_ROOT ), array(".", "..") );
//if there is empty root, return zero. Else, return last folder name;
$id = empty($directory) ? 0 : intval( end($directory) );
return $id;
}
$last_dir = getLastFolderID();
$target_path = IMAGE_ROOT . DIRECTORY_SEPARATOR . $last_dir;
$file_count = count( array_diff( scandir( $target_path ), array(".", "..") ) ); // exclude "." and ".."
//large than 1000 or there is no folder
if( $file_count > 1000 || $last_dir == 0){
$new_name = getLastFolderID() + 1;
$new_dir = IMAGE_ROOT . DIRECTORY_SEPARATOR . $new_name;
if( !is_dir($new_dir) )
mkdir( $new_dir );
}
I use these code at my website, FYR

Parfait
- 1,752
- 1
- 11
- 12
0
So i solved my problem like this.
i'm using laravel for my php development.
first thing i'm getting last pictures folder and then checking if there is more then 1000 images.
if so i'm creating new folder with current date time.
code looks like this.
// get last image
$last_image = DB::table('funs')->select('file')
->where('file', 'LIKE', 'image%')
->orderBy('created_at', 'desc')->first();
// get last image directory
$last_image_path = explode('/', $last_image->file);
// last directory
$last_directory = $last_image_path[1];
$fi = new FilesystemIterator(public_path('image/'.$last_directory), FilesystemIterator::SKIP_DOTS);
if(iterator_count($fi) > 1000){
mkdir(public_path('image/fun-'.date('Y-m-d')), 0777, true);
$last_directory = 'fun-'.date('Y-m-d');
}

Dimi Mikadze
- 474
- 5
- 10