Bruce
Bruce

Reputation: 1071

How to assign unique id to filenames in a directory based on creation time

I have the following function to get all images and videos in a given directory. As can be seen I am using a loop count to assign a unique id to each file.

The problem is, the counter will change based on the filename.

For example zyxw.png may be $data[1]['name'] today but if tomorrow a new file abcd.png is added to the directory, it will then be $data[1]['name'] and zyxw.png would then become $data[2]['name'] because the directory is listed by filename.

I need a way to assign a unique id that never changes. The only way I can think to do this is by assigning the id based on file creation date and time. I realize this could result in collisions if 2 files have the exact same file create date/time but I can think of no other way to do this.

So the questions are:

1: Is there a better option to assign a unique id?

2: How would I do this in such a way each id is unique and the id order doesn't change as more files are added to the directory?

function getDirEnties($directory, &$results, $friend, $static_path) {
    $i = 0;
    $entries = scandir($directory);
    foreach ($entries as $item) {
        if (!in_array($item, ['.', '..']) && substr($item, 0, 1) !== '.') {
            $path = $directory . '/' . $item;
            $rel_path = $static_path . '/' . $item;
            if (is_dir($path)) {
                getDirEnties($path, $results, $friend, $rel_path);
            } else {
                $i++;
                $pathInfo = pathinfo($path);
                $name = $pathInfo['filename'];
                $type = 'unknown';
                if (!empty($pathInfo['extension'])) {
                    $name .= "." . $pathInfo['extension'];
                    switch (strtolower($pathInfo['extension'])) {
                        case "gif":
                        case "jpg":
                        case "png":
                        case "jpeg":
                        case "bmp":
                        //etc..
                        $type = 'image';
                        break;
                        case "mp4":
                        $type = 'video';
                        break;
                    }
                }
                $tttt = filemtime($path);
                $data = [
                    'name' => $name,
                    'username' => $friend,
                    'time' => date('m/d/Y h:i:s a', $tttt),
                    'path' => $rel_path,
                    'type' => $type,
                    'id' => $i,
                ];
                $results[] = $data;
            }
        }
    }
    if (isset($data)) {
        return $data;
    }
}

Upvotes: 1

Views: 1784

Answers (2)

If you are looking for a unique ID for a unique file ( so if the same file is handle later it gets the same id) then hash the file name + file size with a very fast hash algorithm and a small key size and use that as your Id value.

If you want a unique Id for every file handled use a timestamp that include milliseconds.

See this post: https://softwareengineering.stackexchange.com/questions/49550/which-hashing-algorithm-is-best-for-uniqueness-and-speed

Upvotes: 2

Difster
Difster

Reputation: 3260

Timestamp is pretty unique down to the millisecond. But to really ensure no collisions, use the user id plus the timestamp. Because no single user is ever going to save two videos within the exact same millisecond timestamp.

Upvotes: 0

Related Questions