我想为动态创建图像的新文件夹,当一个目录在那里获得1000张图像时使用PHP,MySQL实现这种事情的最佳实践是什么? :)谢谢PHP动态创建文件夹中的千张图像后的新文件夹
0
A
回答
0
所以,我解决我的问题是这样的。
我为我的PHP开发使用laravel。
第一件事我得到最后的照片文件夹,然后检查是否有超过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');
}
0
0
从这个例子Count how many files in directory php
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
>
0
你可以尝试像这样的东西
$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
}
}
+0
谢谢,它是更快计数文件夹或数据库中的图像? – 2014-10-28 11:23:27
+0
是的,它比其他方法快 – 2014-10-28 11:26:25
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);
}
我使用这些代码在我的网站上,前南斯拉夫
相关问题
- 1. 在每张新文章'图片'中创建新文件夹
- 2. 如何移动网站文件夹中的数千张图片?
- 3. 在指定文件夹中创建文件夹在新文件夹中创建子文件夹
- 4. 在asp中创建动态文件夹
- 5. 如何在移动图像时动态创建文件夹?
- 6. 创建文件夹后profile.php/
- 7. PHP创建文件夹($ _POST)
- 8. PHP动态文件夹
- 9. 试图创建$ now文件夹并复制.sql文件创建的文件夹
- 10. 如何在php的特定文件夹中动态创建一个php文件?
- 11. 使用php创建新文件夹
- 12. 创建一个新的文件夹,该文件夹
- 13. 崩溃创建图像文件夹
- 14. 来自文件夹列表的动态图像的PHP循环
- 15. PHP - 创建新子文件夹时文件夹的绝对路径
- 16. 新创建的文件夹上的fsync?
- 17. 移动文件到新创建的子文件夹
- 18. 使用时间戳创建新文件夹,然后将文件移动到新文件夹
- 19. 批量创建子文件夹并将父图像文件复制到新的子文件夹
- 20. 批量创建基于第一部分文件夹的文件夹,将文件移动到该文件夹中
- 21. php创建341权限的文件夹
- 22. PHP不创建该文件夹的mkdir()
- 23. 如何在php中创建文件夹?
- 24. 在C中创建一个文件夹的Zip文件夹#
- 25. 在已经存在的文件夹中创建文件夹
- 26. 列出文件夹中的文件夹 - 然后在JS中为每个文件夹创建数组 - ASP.NET VB
- 27. 为每个文件夹创建x个文件夹的文件夹结构
- 28. 在AN EXISTING驱动器文件夹中创建文件/文件夹
- 29. 在文件系统动态创建文件夹ax
- 30. 如何将图像文件保存或移动到包含在我的php文件夹中的文件夹
'mkdir' => http://php.net/manual/ru/function.mkdir.php – 2014-10-28 11:03:11
是啊,我知道的mkdir的功能,但我想咨询一下不同的东西。如何检查文件夹中的allready是否有1000张图像,然后为一个或多个1000张图像动态创建新文件夹等等。 – 2014-10-28 11:11:18