【问题标题】:Not able to create new directory with variable (PHP)无法使用变量创建新目录(PHP)
【发布时间】:2013-06-30 14:28:57
【问题描述】:

您好,我正在尝试在 users/ 文件夹中创建一个目录。当用户注册时,正在打开一个 vpb_save_details.php。并运行 mkdir 以创建用户名作为目录名称的用户。

用户注册正常,但是没有创建目录的问题,甚至没有创建/user目录。

如果我运行这段代码:

   <?php
   mkdir("blah", 0777);
   ?>

目录 bla 已创建,但如果我运行我的代码 vpb_save_details.php,则没有任何反应。

这是部分代码:

//If the user is validated and every thing is alright, then save the user details and display success message to the user 
            //otherwise, display error message to the user
            if ($vpb_error == '')
            {
                //Encrypt user information with Base64 before saving them to a file
                if(fwrite($vpb_database, "\r\n".base64_encode($fullname)."::::::::::".base64_encode($username)."::::::::::".base64_encode($email)."::::::::::".base64_encode($encrypted_user_password).""))
                {
                    echo '<font style="font-size:0px;">completed</font>';
                    echo '<div class="info">Congrats <b>'.$fullname.'</b>, you have registered successful. <br>You may now click on the login button to log into your account.<br>Thanks.</div>';
                      //
                      // HERE! I create a folder in the users directory with the variable $username as username
                      //
                      $newUserName = $username;
                      $userPath = "users/".$newUserName;
                      mkdir("$userPath", 0777);
                      //copy("index.php",$userPath/index.php);

                }
                else
                {
                    echo '<div class="info">Sorry, your account creation was unsuccessful, please try again (1).</div>';
                }
            }
            else
            {
                echo $vpb_error;
            }
            fclose($vpb_database);

我完全不知道问题可能是什么?代码:

                  $newUserName = $username;
                  $userPath = "users/".$newUserName;
                  mkdir("$userPath", 0777);

应该可以正常工作,还是我遗漏了一个小细节?

【问题讨论】:

  • 用户文件夹是否存在?如果不将您的mkdir 更改为mkdir("$userPath", 0777, true);。如果确实存在,请检查用户目录的权限以确保它是可写的
  • 你确定 if 语句返回 true 吗?
  • @chislonden 成功了,我以为用户目录将由 mkdir 创建...

标签: php mkdir


【解决方案1】:

去掉引号。

mkdir($userPath, 0777);

【讨论】:

  • 引号不会有任何影响。它们是不必要的,但我看不出它们是如何导致问题的
  • 引号会将变量视为字符串。
  • 你的 'if' 条件失败了,但请记住引号不应该在那里。
  • 如果存在引号,将创建一个名为 $userPath 的目录,而不是变量 $userPath 中存在名称的目录。
猜你喜欢
  • 2012-12-04
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2018-02-06
  • 1970-01-01
相关资源
最近更新 更多