简体   繁体   English

Scandir()显示最新文件夹

[英]Scandir() to show the newest folder

I basically have a form that creates a folder when submitted, it takes the last created folder (for example the folder name is 7) and creates a new map based on the last created folder (7+1) thuss making a new folder named 8 etc. etc. 我基本上有一个表单,该表单会在提交时创建一个文件夹,它使用上一个创建的文件夹(例如,文件夹名称为7),并根据上一个创建的文件夹(7 + 1)创建一个新地图,从而创建一个名为8的新文件夹等等等

However when I create a map with the name 10 and echo $latest_dir it will still show 9.. whilst it should just show the highest number at all times. 但是,当我创建一个名称为10的地图并回显$latest_dir它仍将显示9.。而它应该始终显示最高的数字。

$maindir = scandir("uploads/");
$latest_dir = $maindir[0];
$new_dir = $latest_dir+1;

echo $latest_dir;

It probably is a stupid question but I'm not really that good with PHP and this is the only thing that is not working so far. 这可能是一个愚蠢的问题,但是我对PHP的理解并不好,这是迄今为止唯一无法解决的问题。 Any help is greatly appreciated :) 任何帮助是极大的赞赏 :)

This is my code, I don't have tested it but I think it works. 这是我的代码,我没有测试过,但我认为它可以工作。

$maindir = scandir("uploads/");

//remove '.' and '..' folder you can also use a regex to be sur to have only folder with
// number after filter
$mainDir = array_filter($maindir, function ($fileName) {
   if ($fileName !== "." || $fileName !== "..") {
      return TRUE;
   }
   else {
      return FALSE;
   }
}

$maxNumber = 0;

// Browse the array in order get the highest number (maybe you could use natsort() instead
for($i = 0 ; $i < count($mainDir) ; $i++) {
   if($maxNumber > (int) $mainDir[i]) {
      $maxNumber = (int) $mainDir[i];
   }
}

$new_dir = $maxNumber + 1;

I got the problem fixed, the folders were not ordered correctly, I changed my code to this: 我已解决问题,文件夹未正确排序,我将代码更改为此:

$maindir = scandir("uploads/",1);
rsort($maindir);
$latest_dir = $maindir[0];
$new_dir = $latest_dir+1;

It orders the folders correctly and always shows the highest folder name. 它正确排序文件夹,并始终显示最高的文件夹名称。 Thanks for everyone's help :) 谢谢大家的帮助:)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM