Error php permission when uploading [duplicate]

2

I'm trying to upload my files to the lampp server but I have not succeeded, what can be happening?

Warning: move_uploaded_file(../uploads/1579387143.jpg) [function.move-uploaded-file]: failed to open stream: Permission denied in /opt/lampp/htdocs/renan/jrassessoria/admin/paginas/C_Upload.php on line 41

Warning: move_uploaded_file() [function.move-uploaded-file]: Unable to move '/tmp/phpLlJqTq' to '../uploads/1579387143.jpg' in /opt/lampp/htdocs/renan/jrassessoria/admin/paginas/C_Upload.php on line 41

My upload page code is:

<?php

if(isset($_POST['upload'])){

    $file = $_FILES['img'];
    $numfile = count(array_filter($file['name']));

    $folder = '../uploads';

    $permite = array('image/jpeg', 'image/png', 'image/jpg');
    $maxSize    = 1024 * 1024 * 5;

    $msg = array();
    $errorMsg = array(
        1 => 'O arquivo no upload é maior do que o limite definido em upload_max_filesize no php.ini.',
            2 => 'O arquivo ultrapassa o limite de tamanho em MAX_FILE_SIZE que foi especificado no formulário HTML',
            3 => 'o upload do arquivo foi feito parcialmente',
            4 => 'Não foi feito o upload do arquivo' 
    );
    if($numfile <= 0)
            echo 'Selecione uma Imagem!';
    else{
            for($i = 0; $i < $numfile; $i++){
                $name   = $file['name'][$i];
                $type   = $file['type'][$i];
                $size   = $file['size'][$i];
                $error  = $file['error'][$i];
                $tmp    = $file['tmp_name'][$i];

                $extensao = @end(explode('.', $name));
                $novoNome = rand().".$extensao";

                if($error != 0)
                    $msg[] = "<b>$name :</b> ".$errorMsg[$error];
                else if(!in_array($type, $permite))
                    $msg[] = "<b>$name :</b> Erro imagem não suportada!";
                else if($size > $maxSize)
                    $msg[] = "<b>$name :</b> Erro imagem ultrapassa o limite de 5MB";
                else{

                    if(move_uploaded_file($tmp, $folder.'/'.$novoNome))
                        $msg[] = "<b>$name :</b> Upload Realizado com Sucesso!";
                    else
                        $msg[] = "<b>$name :</b> Desculpe! Ocorreu um erro...";

                }

                foreach($msg as $pop)
                    echo $pop.'<br>';
            }
        }

}
?>
    
asked by anonymous 22.06.2015 / 19:16

1 answer

1

Your php process is probably not allowed to write to the directory, go to the folder where you want to save the files and run the commands below.

  

To find out which webserver user can use the whoami command in php

<?php
    //Exemplo para descobrir o usuario_webserver
    echo shell_exec("whoami");

This will call the php file that it will print the user running the process.

Assuming that the folder is / opt / lampp / htdocs / renan / jrassessoria

  

Applying write permissions, use the commands below

sudo chown seu_usuario_do_linux:usuario_webserver_retornado_pelo_codigo_exemplo_acima /opt/lampp/htdocs/renan/jrassessoria -R
sudo chmod 775 /opt/lampp/htdocs/renan/jrassessoria -R
  

Usually the webserver user will be httpd or www-data, it depends on the linux distro you use.

    
22.06.2015 / 19:23