动态为每个用户数据创建一个唯一的目录并检查是否有任何冲突

发布于 2024-11-28 13:04:27 字数 1836 浏览 0 评论 0原文

感谢您的所有帮助 - 请参阅原始问题,以及我遵循两行规则的编辑(作为新用户,我还无法回答我自己的问题..)。

我环顾四周并提出问题(此处)几乎满足了我的问题的目标:

例如,我正在为网络应用程序上的每个用户创建一个用户数据目录;当然,文件夹必须是唯一的,但出于安全考虑,也必须是抽象的(例如,使用其用户 ID 是不合适的。

到目前为止,我已经创建了以下函数;它会生成一个唯一的文件夹名称,检查以确保它不存在并将其分配给一个变量,然后如果该目录已经存在则循环返回:

function generate_unique_userDirectory(){
    $userDirectory = md5(uniqid($uid)); //Generate a unique folder name.
    if (is_dir($userDirectory)) {
        return FALSE; //If the dir exists, report so
    } else {
        return $userDirectory; //Return unique foldername
    }
}

While 循环用于继续执行,直到找到未使用的文件夹名称

while (!$userDirectory = generate_unique_userDirectory()) {
    echo 'folder exists...loop back try another';
    //Try another:
    $userDirectory = generate_unique_userDirectory();
}

。 ,我主要担心的是我是否过于复杂化 时间


非常感谢您的宝贵

重新设计了该功能;

function generate_unique_userDirectory($uid){

        $userDirectory = md5(uniqid($uid));             

            while (is_dir(BASE_URI . "$userDirectory")){

                $userDirectory = md5(uniqid($uid));
            }

            return $userDirectory;

        }//End of generate_unique_userDirectory funcation decleration.

            //Example call to function:
        $userDirectory = generate_unique_userDirectory($uid);

            echo "The generated user directory is: $userDirectory";

谢谢@Veger,以及其他人的帮助,我根据您的建议 将文件夹名称生成 while 循环放在函数内,现在使函数调用变得更加简单

响应您的第二个要点@Veger,据我所知,因为我已经提供了“uniqid”。函数到 md5 函数这将导致每次都用一个新字符串(尽管我可能误解了)。

将 $uid 传递给 generate_unique_userDirectory() 的目的是进一步“加盐”生成的字符串,但是,我可能走得太远了!

非常感谢大家——第一次使用 stackoverflow 真是太棒了……

Thanks for all your help - see blow original question, and my edit following the two line rules (I cannot yet answer my own questions as a new user..).

I've looked around and question (here) almost meets the aims of my question:

For example, I'm creating a user data directory for each user on a web application; the folder of-course must be unique but also abstract for security (using their user id for example, would not be appropriate.

So far I've created the following function; it generates a unique folder name, checks to make sure it doesn't already exist and assigns it to a variable. It then loops back if the dir already exists:

function generate_unique_userDirectory(){
    $userDirectory = md5(uniqid($uid)); //Generate a unique folder name.
    if (is_dir($userDirectory)) {
        return FALSE; //If the dir exists, report so
    } else {
        return $userDirectory; //Return unique foldername
    }
}

While loop is used to keep going until an unused folder name is found.

while (!$userDirectory = generate_unique_userDirectory()) {
    echo 'folder exists...loop back try another';
    //Try another:
    $userDirectory = generate_unique_userDirectory();
}

Is there a better way of doing this, my main concern is whether I'm over-complicating the procedure?

Many thanks for your time.


My findings thanks to all that contributed!

Thanks @Veger, and everyone else; your assistance was brilliant; I've since re-worked the function based on your advice:

function generate_unique_userDirectory($uid){

        $userDirectory = md5(uniqid($uid));             

            while (is_dir(BASE_URI . "$userDirectory")){

                $userDirectory = md5(uniqid($uid));
            }

            return $userDirectory;

        }//End of generate_unique_userDirectory funcation decleration.

            //Example call to function:
        $userDirectory = generate_unique_userDirectory($uid);

            echo "The generated user directory is: $userDirectory";

As suggested, I've put the folder name generation while loop within the function which now makes the function call much simpler.

In response to your second bullet point @Veger, it is my understanding that as I've fed the 'uniqid' function to the md5 function this will result in a new string each time (though I may have misunderstood).

The purpose of passing the $uid to generate_unique_userDirectory() is to further 'salt' the generated string, however, I may be taking it a step too far!

Many thanks to all- a great first time on stackoverflow...

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

戏蝶舞 2024-12-05 13:04:27

明智的解决方案是使用主键或其他一些唯一值。因为这不是使用 uniqid 的选项(实践中冲突应该不存在)是有意义的,但您也可以从给定用户的唯一键生成散列,并可能将其用于查找机制。

$userDir = "/users/" . md5( $user->id . $user->username . $user->email );

如果您想格外小心,请确保不要将使用的数据暴露在任何地方,以免路径无法重建。您还可以通过使用 URL 重写将带有某些参数的请求重定向到 PHP 脚本来增加安全级别。这样您就可以执行诸如检查会话、IP 等操作。

The sensible solution would be to use the primary key or some other unique value. Since that is not an option using uniqid (collisions should be inexistant in practise) would make sense but you could also generate a hash from unique keys for a given user and perhaps use that for a lookup mecanism.

$userDir = "/users/" . md5( $user->id . $user->username . $user->email );

If you want to be extra careful, make sure you don't expose the used data anywhere so the path can't be reconstituted. You can also add a level of security by using url rewriting to redirect requests with certain arguments to a PHP script. That way you can do things like check sessions, IPs and so on.

少钕鈤記 2024-12-05 13:04:27

我对你的设计的一些想法:

  • 正如韦斯利已经评论的那样,generate_unique_userDirectory()表明返回的目录是唯一的。因此,您不需要在调用后检查其结果是否是唯一目录...
  • 第二次调用该函数会导致相同的失败,因为新用户的 $uid 不会更改。
  • 如果它确实发生变化,为什么不将 while 循环放在函数内呢?所以它总是返回一个唯一的目录。
  • 通过在循环本身中调用 while 语句 and 中的函数,会导致该函数在失败后被调用两次。循环中的第一个调用被语句中的调用覆盖。

Some of my thoughts on your design:

  • As Wesley already commented generate_unique_userDirectory() suggests that the returned directory is unique. So you should not need to check after calling whether its result is a unique directory or not...
  • Calling the function a second time results in the same failure, as $uid is not changed for the new user.
  • If it does change, why not put your while loop inside the function? So it always returns a unique directory.
  • By calling the function in the while-statement and in the loop itself, results in it being called twice after failure. First call in the loop which is overridden by the call in the statement.
少女七分熟 2024-12-05 13:04:27

我的建议:

要实现独特的文件夹:

只需使用 user_id 的 md5 创建文件夹即可。

实现安全性:

将文件夹置于脱机状态,而不是放在 Web 路径上,并创建一个类或脚本来访问每个文件资源。

可扩展性

通过使用类或脚本来访问用户文件,您稍后可以将其用作切换到另一种存储机制(如 Amazon S3 或类似云服务)的桥梁。

My suggestions:

To achieve unique folders:

Just create folders with md5 of the user_id.

To achieve security:

Place the folders offline, not on the web path and create a class or script to access each of the file resources.

Scalability:

By having a class or script to access to users files you can later use it as bridge to switch to another storage mechanism like Amazon S3 or similar cloud services.

飘过的浮云 2024-12-05 13:04:27

恕我直言,这是正常的做法。由于 md5 碰撞的可能性太低,因此大多数情况下都会进行一次迭代。有人认为我不喜欢您的代码:

if (is_dir($userDirectory)) {
    return FALSE; //If the dir exists, report so
} else {
    return $userDirectory; //Return unique foldername
}

可能存在同名的常规文件。最好用 file_exists() 检查

imho, it's normal aproach. there will be mostly one iteration since md5 has too low possiblity of collision. one think i don't like in your code:

if (is_dir($userDirectory)) {
    return FALSE; //If the dir exists, report so
} else {
    return $userDirectory; //Return unique foldername
}

there might exists regular file with the same name. it's better to check with file_exists()

沙沙粒小 2024-12-05 13:04:27
   function generate_unique_userDirectory($uid){
        $userDirectory = md5(uniqid($uid.time())); //Generate a unique folder name.
        if (is_dir($userDirectory)) {
            return generate_unique_userDirectory($uid); //If the dir exists, report so
        } else {
            return $userDirectory; //Return unique foldername
        }
    }

为什么不让你的函数递归呢?如果生成的目录已在使用,脚本将继续尝试,直到生成唯一的目录。这应该是函数的结果,因此得名;-)

   function generate_unique_userDirectory($uid){
        $userDirectory = md5(uniqid($uid.time())); //Generate a unique folder name.
        if (is_dir($userDirectory)) {
            return generate_unique_userDirectory($uid); //If the dir exists, report so
        } else {
            return $userDirectory; //Return unique foldername
        }
    }

Why not make your function recursive? If the generated directory is already in use, the script will keep on trying untill it generates an unique directory. This should be the result of the function, hence the name ;-)

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文