Error php permission while uploading

Asked

Viewed 1,233 times

2

I’m trying to upload my files to the lampp server but I haven’t been able to, which may be occurring ?

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

}
?>
  • 1

    have you checked if the folder you are saving has written permission ? (chmod 777)

  • Vlw bug fixed !

  • 777 cannot, of the correct permissions

  • I put in the reply

1 answer

1


Your php process is probably not allowed to write in 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");

Done this call the php file it will print the user who is running the process.

Supposing that the folder is /opt/lampp/htdocs/Renan/jrassessoria

Applying write permissions, for this 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, depending on the linux distro you use.

Browser other questions tagged

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