【问题标题】:PHP Create New Folder Dynamically After Thousand Images In FolderPHP在文件夹中的千张图像后动态创建新文件夹
【发布时间】:2014-12-23 18:10:31
【问题描述】:

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

【问题讨论】:

  • 是的,我知道 mkdir 函数,但我问的是不同的事情。如何检查文件夹中是否已经有 1000 张图像,然后为另外 1000 张图像动态创建新文件夹,依此类推。

标签: php file upload max directory


【解决方案1】:

要计算文件夹中的文件数,请参阅answer

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

所以你会有类似的东西:

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

if ( $files !== false )
{
    $filecount = count( $files );
    if ($filecount >= 1000)
    {
        mkdir('images_2');
    }
}

【讨论】:

    【解决方案2】:

    从这个例子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
    

    ?>

    【讨论】:

      【解决方案3】:

      你可以试试这样的

      $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
          }
      }
      

      【讨论】:

      • 是的,它比其他方法更快
      【解决方案4】:
      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 );
      
      }
      

      我在我的网站上使用这些代码,FYR

      【讨论】:

        【解决方案5】:

        所以我这样解决了我的问题。

        我正在使用 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');
        } 
        

        【讨论】:

          猜你喜欢
          • 1970-01-01
          • 1970-01-01
          • 2014-06-21
          • 2013-11-12
          • 2013-11-18
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2013-07-14
          相关资源
          最近更新 更多