【问题标题】:I can not create a folder in php我无法在 php 中创建文件夹
【发布时间】:2015-02-16 11:35:56
【问题描述】:

我正在开发一个应用程序,如果满足某个条件,可以通过按下按钮来创建文件夹。问题是该文件夹是使用 mkdir () 创建的,不明白为什么。甚至 html 页面都告诉我该文件夹已创建,但这并没有出现在目录中。我不明白这个错误。代码如下:

<html>
 <head>
   <title>RHM</title> 
   <style type="text/css">
      h1 { color: red; font-family: arial; font-size: 3em; font-weight: bolder; }
      p { color: navy; font-family: Verdana; }
   </style>
 </head>

 <body>
     <h1 align="center">INGRESE CONTRASE&Ntilde;A</h1>

     <form  action="#" method="post" >
        <p align="center"> <input type="password" name="contras" style="width:200px;height:50px;background-color:yellow;color:blue;font-size:14pt;font-family: Comic Sans MS;text-align:center;padding-right:10px;"/></p>
        <p align="center"  ><input type="submit" value="Entrar" /></p>

     <?php
        $Contraseña=$_POST['contras'];
        $estructura = "/home/bladimir/RHMbd";
        if ($Contraseña==1) {
            mkdir($estructura);
            echo "<p>La carpeta fue creada</p>";
        }
     ?>

     </form>
  </body>
</html>

谢谢。

【问题讨论】:

  • 当你说«但是这并没有出现在目录中»时,您是在检查服务器端还是客户端? php用户是否有权创建子目录?
  • 你应该指定第三个参数递归 true 到 mkdir
  • 您可能还想分享错误。据推测,您没有对/home/bladimir 的写入权限。检查该文件夹的权限,特别是写入权限。
  • 你得到了La carpeta fue creada,因为$Contraseña==1 正在变为真实,它没有检查目录是否已创建..

标签: php directory mkdir


【解决方案1】:

我猜你没有创建目录的权限。

转到文件夹 /private/etc/apache2

打开 httpd.conf

查找

User _www 
Group _www

更改用户名:

User <YOUR LOGIN USERNAME>

重启apache。

【讨论】:

  • 0777 是默认的,即使你没有指定它也会被默认采用
  • 试试上面的代码,用你的代码替换它,告诉我它是否有效
  • 服务器位于我的本地计算机上。添加第三个递归参数不起作用。建议的代码不起作用。
  • 我在 64 位电脑上使用 debian 7 和 eclipse。目录/private/etc/apache2 httpd.conf文件并在我的电脑中不存在。
【解决方案2】:
<?php
    if (isset($_POST['contras']))
    {
        $Contraseña = $_POST['contras'];
        $estructura = "c:://home/bladimir/RHMbd";

        $dir = dirname($estructura);

        if (!is_dir($dir))
        {
            var_dump(mkdir($dir, 0777, true));
            if ($Contraseña == '1')
            {
                echo 'fsdf';
                mkdir($estructura);
                echo "<p>La carpeta fue creada</p>";
            }
        }
    }

    ?>

【讨论】:

  • 包含一些解释会很有帮助。
【解决方案3】:

我通过在控制台中使用以下命令向用户授予适当的权限来解决它:chmod a + w bladimir。谢谢大家。

【讨论】:

    猜你喜欢
    • 2014-11-12
    • 1970-01-01
    • 1970-01-01
    • 2017-12-10
    • 2016-11-17
    • 2014-11-30
    • 2015-03-23
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多