Permissions error when trying to send a PHP file

Asked

Viewed 1,004 times

0

<html>
<body>
    <form action="" method="post" enctype="multipart/form-data">
        Selecione : <input name="arquivo" type="file">
        <input type="submit" value="Enviar">
    </form>

    <?php
    //Salva a foto com nome aleatório dentro da pasta
    $pasta = 'pasta-upload';

    $arq = $pasta . '/' . uniqid(rand(), true);

    $image = false;

    if (!chmod($pasta, 0755)) {
        echo 'Erro ao mudar as permissões';
    } else if (isset($_FILES['arquivo']['tmp_name'])) {
        if ($_FILES['arquivo']['error'] !== UPLOAD_ERR_OK) {
            echo 'Erro no upload';
        } else {
            $tmp = $_FILES['arquivo']['tmp_name'];

            $image = getimagesize($tmp);

            $ext = str_replace(array('image/', 'x-'), '', $image['mime']);

            $arq .= '.' . $ext;

            if (!$image) {
                echo 'Formato invalido';
            } else if (!move_uploaded_file($tmp, $arq)) {
                echo 'Erro ao mover o arquivo';
            } else {
                echo '<img src="', $arq, '">';
            }
        }
    }
    ?>
   </body>
</html>

Returns the following

Warning: chmod(): Operation not permitted in /Applications/XAMPP/xamppfiles/htdocs/tm/index.php on line 16 Error while changing permissions.

I’ve tried going to the terminal and executing the commands

sudo chmod 777 /Applications/XAMPP/xamppfiles/htdocs/tm/pasta-upload   
cd /Applications/XAMPP/xamppfiles/htdocs/tm/
sudo chmod 755 /Applications/XAMPP/xamppfiles/htdocs/tm/pasta-upload

I’m using the XAMPP.

  • The error persists because, even if you have changed the directory permissions, you are trying to change the code again (and it is the code that has no permissions to make this change). Try to replace the code part if (!chmod($pasta, 0755)) for if (!is_writable($pasta)).

  • worked thanks

1 answer

2


For references, the code in question was developed in this question.

What happens is that in the code, more precisely in line !chmod($pasta, 0755), you are trying to change the permissions of the defined directory $pasta, in order to ensure that it can be saved. The error is generated because PHP does not have the necessary permissions to make this change. That is why it was necessary for you to execute this command manually:

sudo chmod 755 /Applications/XAMPP/xamppfiles/htdocs/tm/pasta-upload

Done this, you have already set the write permission in the directory, so in the code you do not need to do it again, but what you need is to ensure that the directory has this permission and this is done with the function is_writable. This way, your code would be:

...

if (!is_writable($pasta)) {
    echo 'O diretório não possui permissão de escrita.';
} else if (isset($_FILES['arquivo']['tmp_name'])) {
    if ($_FILES['arquivo']['error'] !== UPLOAD_ERR_OK) {
        echo 'Erro no upload';
    } else {
        $tmp = $_FILES['arquivo']['tmp_name'];

        $image = getimagesize($tmp);

        $ext = str_replace(array('image/', 'x-'), '', $image['mime']);

        $arq .= '.' . $ext;

        if (!$image) {
            echo 'Formato invalido';
        } else if (!move_uploaded_file($tmp, $arq)) {
            echo 'Erro ao mover o arquivo';
        } else {
            echo '<img src="', $arq, '">';
        }
    }
}

...

This way, sending the file only occurs when the directory has the necessary permissions.

  • So PHP does not have permission because the proprietary user is not the same, if I am not mistaken the user would be the www-data, I was going to comment on this in response, but I was going to test first on work Mac.

Browser other questions tagged

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