-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathgetnewid.php
65 lines (52 loc) · 1.57 KB
/
getnewid.php
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<?php
// what this file does: searches through the folder structure on /output, finds the next ID in the lot, creates that folder and returns that ID to be used elsewhere
// note: if 2 users hit this at the exact same time there will be a collision
function GetNewId() {
$path = "./output";
$errorMsg = "";
// get a list of folders
$folders = [];
if ( is_dir($path) ) {
if ( $dh = opendir($path) ) {
while ( ($file = readdir($dh)) !== false ) {
if ( filetype($path . "/" . $file) == "dir" ) {
if ( (int)$file > 0 ) {
$folders[] = (int)$file;
}
}
}
}
}
if ( count($folders)> 0 ) {
sort($folders);
$lastDir = end($folders);
$id = (int)$lastDir + 1;
//$errorMsg .= "<p>folders: " . implode(", ", $folders) . "</p>\n";
}
$haveId = false;
if ( isset($id) ) {
if ( is_int($id) ) {
$haveId = true;
} else {
$errorMsg .= "<p>Last folder found can't be converted to int ($id).</p>\n";
}
}
if ( ! $haveId ) {
$id = 1;
}
$isDirSet = false;
if ( mkdir($path . "/" . $id) ) {
$isDirSet = true;
} else {
// the rare instance there was a collision, try it again but skip up a bunch. This is hacky
$id += rand(1, 20);
if ( mkdir($path . "/" . $id) ) {
$isDirSet = true;
}
}
if ( $isDirSet ) {
$_SESSION['folder_id'] = $id;
}
return $errorMsg;
}
?>