PHP在文件夹中创建上千个图像后动态创建新文件夹
我想动态地为图像创建一个新文件夹,当一个目录中有1000个图像时,使用PHP、MySQL实现这种效果的最佳实践是什么感谢您计算文件夹中的文件数量,我建议您参考此 然后使用该函数创建一个新目录 所以你会有这样的想法:PHP在文件夹中创建上千个图像后动态创建新文件夹,php,file,upload,max,directory,Php,File,Upload,Max,Directory,我想动态地为图像创建一个新文件夹,当一个目录中有1000个图像时,使用PHP、MySQL实现这种效果的最佳实践是什么感谢您计算文件夹中的文件数量,我建议您参考此 然后使用该函数创建一个新目录 所以你会有这样的想法: $directory = 'images'; $files = glob($directory . '*.jpg'); if ( $files !== false ) { $filecount = count( $files ); if ($filecount &g
$directory = 'images';
$files = glob($directory . '*.jpg');
if ( $files !== false )
{
$filecount = count( $files );
if ($filecount >= 1000)
{
mkdir('images_2');
}
}
从这个例子中
添加一个if语句,当文件达到一定数量时,该语句将创建一个文件夹
<?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
你可以试试这样的东西
$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
}
}
我在我的网站FYR中使用了这些代码,所以我像这样解决了我的问题
// 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');
}
我正在使用laravel进行php开发
第一件事我得到最后的图片文件夹,然后检查是否有超过1000张图片
如果是这样,我将使用当前日期和时间创建新文件夹
代码看起来像这样
// 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');
}
mkdir=>是的,我知道mkdir函数,但我问的是不同的事情。如何检查文件夹中的allready是否为1000个图像,然后为一个或多个1000个图像动态创建新文件夹,依此类推。是的,它比其他方法更快