散列 PHP 中创建的文件夹的名称?
Hash the name of a created folder in PHP?
所以我下面的 PHP 代码获取用户的 ID 并检查该用户在服务器上是否有个人资料文件夹,他们的所有个人资料数据最终都将存储在那里——如果用户还没有,它会根据用户的用户名自动创建一个文件夹。
我想这样做,这样文件夹的名称就不会与用户的个人资料名称完全相同。我想散列它。我很难做到这一点,并且想知道是否有一种方法可以向这段代码中添加一个哈希函数来对创建的文件夹进行哈希处理。我更喜欢 sha256 哈希算法,但我不确定这是否是这种情况下的最佳选择,或者它是否重要。 P.S.: 我已经尝试过对变量进行哈希处理,但显然我在这里做错了。代码如下:
<!-- The following checks if the user already has a profile folder, and if not, it creates one equal to the username of the user -->
<?php
$userID = $_SESSION["username"];
// Define path where file will be uploaded to
// User ID is set as directory name
$profileFolder = "profiles/$userID";
hash('sha256', $profileFolder);
// Check to see if directory already exists
$exist = is_dir($profileFolder);
// If directory doesn't exist, create directory
if(!$exist) {
mkdir("$profileFolder");
chmod("$profileFolder", 0755);
}
else { echo "<p id='welcome-msg'>Welcome, <p id='userfolder'>$userID</p></p>"; }
?>
我已经知道代码有效,至少对于 checking/creating 用户文件夹的主要目的是有效的。我只是不希望创建的文件夹与用户的用户名完全相同。
显然,您正在散列整个路径,而不仅仅是用户文件夹。
$profileFolder = hash('sha256', $userID);
$hashed_folder = "profiles/{$profileFolder}" ;
// Check to see if directory already exists
$exist = is_dir($hashed_folder);
不确定我是否应该回复我自己的 post 以展示有效的解决方案,但我尝试了这个,这是有效的,而且非常完美,谢谢你们:
<?php
$userID = $_SESSION["username"];
// Define path where file will be uploaded to
// User ID is set as directory name
$hashedFolder = hash('sha256', $userID);
$profileFolder = "profiles/$hashedFolder";
// Check to see if directory already exists
$exist = is_dir($profileFolder);
// If directory doesn't exist, create directory
if(!$exist) {
mkdir("$profileFolder");
chmod("$profileFolder", 0755);
}
else { echo "<p id='welcome-msg'>Welcome, <p id='userfolder'>$userID</p></p>"; }
?>```
所以我下面的 PHP 代码获取用户的 ID 并检查该用户在服务器上是否有个人资料文件夹,他们的所有个人资料数据最终都将存储在那里——如果用户还没有,它会根据用户的用户名自动创建一个文件夹。
我想这样做,这样文件夹的名称就不会与用户的个人资料名称完全相同。我想散列它。我很难做到这一点,并且想知道是否有一种方法可以向这段代码中添加一个哈希函数来对创建的文件夹进行哈希处理。我更喜欢 sha256 哈希算法,但我不确定这是否是这种情况下的最佳选择,或者它是否重要。 P.S.: 我已经尝试过对变量进行哈希处理,但显然我在这里做错了。代码如下:
<!-- The following checks if the user already has a profile folder, and if not, it creates one equal to the username of the user -->
<?php
$userID = $_SESSION["username"];
// Define path where file will be uploaded to
// User ID is set as directory name
$profileFolder = "profiles/$userID";
hash('sha256', $profileFolder);
// Check to see if directory already exists
$exist = is_dir($profileFolder);
// If directory doesn't exist, create directory
if(!$exist) {
mkdir("$profileFolder");
chmod("$profileFolder", 0755);
}
else { echo "<p id='welcome-msg'>Welcome, <p id='userfolder'>$userID</p></p>"; }
?>
我已经知道代码有效,至少对于 checking/creating 用户文件夹的主要目的是有效的。我只是不希望创建的文件夹与用户的用户名完全相同。
显然,您正在散列整个路径,而不仅仅是用户文件夹。
$profileFolder = hash('sha256', $userID);
$hashed_folder = "profiles/{$profileFolder}" ;
// Check to see if directory already exists
$exist = is_dir($hashed_folder);
不确定我是否应该回复我自己的 post 以展示有效的解决方案,但我尝试了这个,这是有效的,而且非常完美,谢谢你们:
<?php
$userID = $_SESSION["username"];
// Define path where file will be uploaded to
// User ID is set as directory name
$hashedFolder = hash('sha256', $userID);
$profileFolder = "profiles/$hashedFolder";
// Check to see if directory already exists
$exist = is_dir($profileFolder);
// If directory doesn't exist, create directory
if(!$exist) {
mkdir("$profileFolder");
chmod("$profileFolder", 0755);
}
else { echo "<p id='welcome-msg'>Welcome, <p id='userfolder'>$userID</p></p>"; }
?>```