Permissions error when trying to send a file with PHP

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 have tried to go to the terminal and execute 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 XAMPP.

    
asked by anonymous 06.05.2017 / 15:43

1 answer

2

For references, the code in question has been developed in this question / a>.

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

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

Once you have done this, you have already set the write permission on the directory, so in the code you do not need to do it again, but you need to ensure that the directory has this permission, and this is done with the is_writable function. So your code would look like this:

...

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, '">';
        }
    }
}

...

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

    
06.05.2017 / 16:10