2014-10-28 108 views
0

我想为动态创建图像的新文件夹,当一个目录在那里获得1000张图像时使用PHP,MySQL实现这种事情的最佳实践是什么? :)谢谢PHP动态创建文件夹中的千张图像后的新文件夹

+1

'mkdir' => http://php.net/manual/ru/function.mkdir.php – 2014-10-28 11:03:11

+0

是啊,我知道的mkdir的功能,但我想咨询一下不同的东西。如何检查文件夹中的allready是否有1000张图像,然后为一个或多个1000张图像动态创建新文件夹等等。 – 2014-10-28 11:11:18

回答

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

要计算一个文件夹内的文件数,我把这个answer

然后,您将使用mkdir()函数创建一个新目录。

所以,你会碰到这样的:?

$directory = 'images'; 
$files = glob($directory . '*.jpg'); 

if ($files !== false) 
{ 
    $filecount = count($files); 
    if ($filecount >= 1000) 
    { 
     mkdir('images_2'); 
    } 
} 
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); 

} 

我使用这些代码在我的网站上,前南斯拉夫

相关问题