警告:mkdir():托管程序中的权限被拒绝

Warning: mkdir(): Permission denied in hostinger

我试图用 php 和 mkdir() 函数自动创建目录。

<?php
session_start();

$domain = $_SESSION['domain'];
$mydomain = "/" . $domain;
echo $_SESSION['domain'] . " " . $mydomain . "<br />";
$mk = mkdir($mydomain, 0777, true);
if ($mk){
    echo "directory created";
}else{
    echo "directory no created";
}
?>

但它给了我这个错误

[02-May-2021 09:19:41 UTC] PHP Warning:  mkdir(): Permission denied in /home/u950517783/domains/mydomain.host/public_html/website/createdir.php on line 7
[02-May-2021 09:22:28 UTC] PHP Warning:  mkdir(): Permission denied in /home/u950517783/domains/mydomain.host/public_html/website/createdir.php on line 7
[02-May-2021 09:22:30 UTC] PHP Warning:  mkdir(): Permission denied in /home/u950517783/domains/mydomain.host/public_html/website/createdir.php on line 7
[02-May-2021 09:22:31 UTC] PHP Warning:  mkdir(): Permission denied in /home/u950517783/domains/mydomain.host/public_html/website/createdir.php on line 7
[02-May-2021 09:22:32 UTC] PHP Warning:  mkdir(): Permission denied in /home/u950517783/domains/mydomain.host/public_html/website/createdir.php on line 7
[02-May-2021 09:22:32 UTC] PHP Warning:  mkdir(): Permission denied in /home/u950517783/domains/mydomain.host/public_html/website/createdir.php on line 7
[02-May-2021 09:22:36 UTC] PHP Warning:  mkdir(): Permission denied in /home/u950517783/domains/mydomain.host/public_html/website/createdir.php on line 7

我该如何解决? 我正在使用主机

你可以试试下面的方法

<?php
 session_start();

 $domain = $_SESSION['domain'];
 $mydomain = "/" . $domain;
 echo $_SESSION['domain'] . " " . $mydomain . "<br />";

 $old = umask(0);
 $mk = mkdir($mydomain, 0777, true);
 umask($old);

  if ($mk){
      echo "directory created";
  }else{          
      chmod($mydomain, 0777);
  }
 ?>

如果 mkdir() 无效,您需要使用 chmod()

更改权限

https://www.php.net/umask

例如使用echo查看$mydomain的值,然后使用此命令授予权限

chmod 777 -R <directory name>