Error creating folder in PHP

Asked

Viewed 75 times

-1

I’m having trouble using the mkdir() inside my vps in the Digital Ocean. I installed the LAMP and I made all the appropriate settings to run my system. However, in the folder creation area, it is not working. Follow the code:

$pasta = 'teste';
$path = '/var/www/meusite.com.br/' . $pasta;

if (!mkdir($path, 0755, true)) 
{
    echo('Não foi possível criar a pasta');
}
else
{
    echo('Pasta criada com sucesso!');
}

The return is always FALSE.

Is there any error variable in this case that I can identify why it cannot create?

Any idea what it might be? I’m using version 7.2 of PHP.

  • 1

    The briefcase /var/www/meusite.com.br/ have written permission for the group www-data?

  • @Erloncharles really was permission. Permission was only in the user and not in www-data. Thanks for the help friend!

  • I will enter an answer for registration only

1 answer

1


So that the php can perform file and directory creation actions, so make sure your folder /var/www/meusite.com.br/ has permission for the group www-data.

Set this permission with the command:

sudo chown -R {usuario_logado}:www-data /var/www/meusite.com.br/
  • Complementing: no Laravel, I use the sudo chmod -Rf 777 /var/www/meusite.com.br/ and also works when I have problems related to permission.

  • 1

    @Cypherpotato 777 you are giving permission to any user to do anything, which is a clear example of lack of security, the folders must be with permission 755 and the files with permission 644 and all with permission for the group www-data so that the php can perform folder and file creation actions.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.