Search code examples
phphostingpermission-denied

Warning: mkdir(): Permission denied in hostinger


I was trying to create directories automatically with php, with the mkdir() function.

<?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";
}
?>

but it gave me back this error

[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

how can i fix that? im using hostinger


Solution

  • You can try like below

    <?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);
      }
     ?>
    

    if mkdir() Not worked, You need to change permissions using chmod()

    https://www.php.net/umask