isDot()) continue; $current = pathinfo($fileInfo->getFilename())['filename']; if (!is_numeric($current)) continue; if ($current > $max) $max = $current; } //Check if the number of files exceed the max if ($max > 1000){ foreach (new DirectoryIterator('Memo/') as $fileInfo) { $current = pathinfo($fileInfo->getFilename())['filename']; if (!is_numeric($current)) continue; if ((int)$current != 0){ unlink('Memo/' . $current . ".txt"); } } $max = 0; } $filename = (int)$max + 1; $myfile = fopen($memodir . $filename . ".txt", "w") or die("Unable to open file!"); $txt = $content; fwrite($myfile, $txt); fclose($myfile); echo $filename; ?>