S
S
Stepan2020-07-29 20:53:11
linux
Stepan, 2020-07-29 20:53:11

Why does a Permission denied error occur when deleting a PHP file?

First I copy the files to one becup2 folder

$arr = myscandir("./");
$arr2=array();
foreach ($arr as $file) {
    if (preg_match('/(.*)\.(.+)/m', $file)){
        array_push($arr2,$file);
    }
};
 
 
foreach ($arr2 as $file) {
    $new_file = './becup2/'.$file;
    if (copy($file, $new_file)) {
        echo 'Файл успешно скопирован!';
    }else{
        echo 'Файл не удалось скопировать!';
    }
    chmod($new_file, 0750);
}

But I also need the ability to delete files from this folder. But when I do unlink($file) , php returns an error:
Warning: unlink(.htaccess): Permission denied in /home/Stepashka20/public_html/test.php on line 26

Apache runs as user www-data . I tried to give the folder all rights (777) and set it to chown www-data:www-data becup2 , but again it doesn’t work. The files in the folder have a user and group - www-data
How to allow deletion files?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dmitry, 2020-07-29
@gebrak

Check the permissions of the file .htaccess. Often, his rights are further restricted.
Another possible reason is SELinux

K
kocherman, 2020-07-29
@kocherman

Is SELinux enabled on the server ?
If yes, then the folder needs to set the desired context.
Something like this (fill in your paths):

semanage fcontext -a -t httpd_sys_script_exec_t '/var/www/html(/.*)?'
restorecon -R -v /var/www/html/

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question