Error upload PHP image. Warning: move_uploaded_file(): failed to open stream: Permission denied

Asked

Viewed 1,406 times

0

Error you are showing when I execute the code:

Warning: move_uploaded_file(fotos/7d80310d2fa83125d395d77e6f739bb8.png): failed to open stream: Permission denied in /var/www/PHP/Diego/Funcoes_PHP/upload_imagem/upload.php on line 15 Warning: move_uploaded_file(): Unable to move '/tmp/phpvi9j6k' to 'fotos/7d80310d2fa83125d395d77e6f739bb8.png' in /var/www/PHP/Diego/Funcoes_PHP/upload_imagem/upload.php on line 15 erro

The code I’m using is just below:

<?php
    include ("conexao.php");

    $msg = false; 

    if(isset($_FILES['arquivo'])){
        $extensao = strtolower(substr($_FILES['arquivo']['name'], -4)); 

        $novo_nome = md5(time()).$extensao;
        $diretorio = 'fotos/'; //diretório para o upload

        /move_uploaded_file(filename, destination)
        if(move_uploaded_file($_FILES['arquivo']['tmp_name'], $diretorio.$novo_nome)){
            echo 'Arquivo salvo com sucesso em : <strong>' . $diretorio . '</strong><br />';
                    echo ' < img src = "' . $novo_nome . '" />';
        }else{
            echo 'erro';
        }

        $sql_code = "INSERT INTO arquivo (codigo, arquivo, data) values (null, '$novo_nome', NOW())"; //função now() pega o horário atual


        mysql_query($sql_code,$conexao) or die (mysql_error(""));

    } 
?>
<?php if($msg!= false) echo "<p> $msg </p>";?>
<h1>Upload</h1>
<form action="upload.php" method="POST" enctype="multipart/form-data" >
    Arquivo: <input type="file" required name="arquivo">
    <input type="submit" value="Salvar">
</form>
  • 1

    Confirmed syntax error: https://www.php.net/manual/en/function.move-uploaded-file.php

  • Diego, why create two identical questions?

1 answer

1

Diego, you are having folder permission problem.

So that you can move file to this directory: $diretorio = 'fotos/'; It is necessary that the same has permission for this.

With this, you can simply do chmod(), where you can modify the folder permission. Do so before this line (

$diretorio = 'fotos/'; //diretório para o upload

) in your code: chmod ("/fotos/", 0777); where 0777 is the total permission in the folder.

Thus remaining:

chmod ("/fotos/", 0777);
$diretorio = 'fotos/'; //diretório para o upload

And the rest of your code

  • And what should be taken into account when giving permission 777, which allows everything? What are the security loopholes in this? Is this an operation that should be executed in PHP? Because every time an upload is made the permissions will be defined; it would not be enough just once?

Browser other questions tagged

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