"unlink permission denied" error trying to delete deleteme.txt - php

I want to delete deleteme.txt in the Ubuntu web server.
So I made 4.php do the following:
<?php
unlink('deleteme.txt');
?>
deleteme.txt has the following permission status:
-rwxrwxrwx 1 ubuntu ubuntu 19 Jun 12 06:18 deleteme.txt
When I execute "4.php", this error always occurs
Warning: unlink(deleteme.txt): Permission denied in /var/www/html/4.php on line 2
I already tried chmod 777 deleteme.txt and chown ubuntu /var/www/html on the directory which contains "deleteme.txt"
I also tried chown ubuntu /var/www/ on the parent directory of that file.

you need to chown to www-data, thus meaning that the www-data will gain ownership of the file allowing you to delete it through unlink with php.
Like so:
$ chown www-data <file or folder>

Related

fopen() works properly in a debug session, but doesn't work when I call the file through the browser

I'm new to PHP and just faced the problem with creating and opening a file with fopen(). Here is my code:
<?php
$new_file = fopen('file.txt', 'w') or die("Cannot create a file");
$text = <<<_END
Line 1
Line 2
Line 3
_END;
fwrite($new_file, $text) or die('Cannot write to the file');
fclose($new_file);
When I try to run the file by opening it in the browser I see the next message: 'Cannot create a file'. But when I start debug session everithing works as it supposed to. I suspect that there is some issue with permissions and XDebug uses root access unlike the usual interpreter?
to write and read permission
chown -R www-data:www-data /var/www/dicrctoy
chmod -R 775 /var/www/dicrctoy
I have tested in my local environment no error was found
if you're using mac you should check the file permission
use chmod command to change permission
What is the meaning of chmod 777?
readable, writable and executable
Setting 777 permissions to a file or directory means that it will be readable, writable and executable by all users
To solve this problem first of all I was needed to check PHP user with the next command:
<?php echo `whoami`; ?>
It outputs:
www-data
This is the default PHP user. Next I checked the owner of the folder with this command:
ls -dl /var/www/html/test
It outputs:
drwxrwxr-x 2 username username 4096 Jun 26 12:49
Next I've sat permissions to the PHP user by running:
sudo chown -R www-data /var/www/html/test
Checking once again if the owner changed
ls -dl /var/www/html/test
And now it outputs
drwxrwxr-x 2 www-data username 4096 Jun 26 12:55
Done. Now I'm able to create and write to the file.

Permission denied when moving file

I am trying to move file to another folder but it gives Permission denied error. Here is the permission of that files.
-rwxrwxrwx 1 root www-data 394 Oct 11 14:40 namechange.xml*
-rwxrwxrwx 1 root www-data 395 Oct 11 14:40 namechange1.xml*
-rwxrwxrwx 1 root www-data 345 Oct 11 14:40 roomchange.xml*
and here id the code that I used to move files
rename("resources/xml_checkin/namechange.xml", "resources/xml_checkout/test.xml")
;
Follow these steps:
Please open your terminal
Login with ssh
locate to the destination file using cd /var/www/html
Give permission to the particular folder using command chmod -R 777 path/folder or command chmod -R 777 path/filename
First try to change the chmod to 775, if it doesn't work then use 777
then try to move your files and see the result.
If issue persists again, let me know.

File Permissioning Error for Apache on Linux

I'm trying to get a simple PHP file to work on a linux Centos 7 server using apache. The problem is that the php code doesn't seem to have permission to write to the folder. The simple test php file below illustrates the problem
<?php
echo shell_exec('whoami');
echo "<br>";
$myfile = fopen("test.txt","w") or die("could not open test file");
fclose($myfile);
?>;
Just to try to get it to work I have done
sudo chmod -R a+rwx /var/www
and yet I keep getting the "could not open test file" error message. What am I doing wrong? Incidentally, the 'whoami' is coming back as 'apache'
**Edit*
In the light of the suggestion below I've done some changes and am now showing the full permissioning for the folders. I've created the group www-data and have added the user apache to it.
[prompt]$ groups apache
apache : apache www-data
For /var/www:
0 drwxrwsrwx. 4 root www-data 33 Jul 27 08:19 www
For /var/www/html:
0 drwxrwsrwx. 2 root www-data 137 Jul 27 12:43 html
The file I'm trying to load:
4 -rwxrwxrwx. 1 root www-data 182 Jul 27 12:40 test.php
It's still not working unfortunately. Might it be something in the apache configuration? Any suggestions would be much appreciated
Here are two options you can try
Option 1
Make sure the group is www-data on '/var/www'.
prompt> sudo chgrp www-data /var/www
Make '/var/www' writable for the group.
prompt> sudo chmod 775 /var/www
Set the GID for www-data for all sub-folders.
prompt> sudo chmod g+s /var/www
Your directory should look like this on an 'ls -l' output.
drwxrwsr-x
Last, add your user name to the www-data group (secondary group).
prompt> sudo useradd -aG www-data [USERNAME]
Option 2
Use the mod_userdir as described in https://httpd.apache.org/docs/2.4/mod/mod_userdir.html
I would recommend the first option as it suits your needs better.

Php Error : failed to open stream: Permission denied in CentOS 7.1.1503(Core)

I am running Apache/2.4.6 (CentOS) build Aug 24 2015 18:11:25 on CentOS Linux release 7.1.1503 (Core) 64-bit. I am facing an issue while opening a file in 'w' mode and in 'a' mode also. I searched a lot for it. Tried all mentioned steps but still I am not getting desired result. The same file works and allows me to be opened in 'w' mode as well as in 'a' mode in my Windows 10. But it's not working in CentOS. There is file named 'home.php' in the directory '/var/www/html/editable'. When I try to open that file from the file srvr_file_access.php in 'w' mode or 'a' mode, I get the following error:
PHP Warning: fopen(/var/www/html/editable/home.php): failed to open
stream: Permission denied in /var/www/html/srvr_file_access.php
I have changed the owner and group of that file and directory to 'apache' and have assigned permission 755. The permission for 'home.php' file and directory are as follow :
home.php
-rwxr-xr-x+ 1 apache apache 0 Oct 11 14:41 home.php
/var/www/html/editable
drwxr-xr-x+ 2 apache apache 21 Oct 11 14:56 editable
Please can anyone help me to solve this issue ?
Thank you.
For anyone that still have this problem, i solved it by creating the folder in /var then use chown apache:apache folder_name, and then move in www folder.
cd [laravelfolder]
chmod 777 storage -R
sudo chcon -t httpd_sys_rw_content_t storage -R
chmod 777 bootstrap
sudo chcon -t httpd_sys_rw_content_t bootstrap -R

Unable to save a file due to file permissions

I'm trying to upload and save an image file. This has worked fine in the past, but is now returning an error.
Warning: move_uploaded_file(//home/bitnami/htdocs/lookgram/photos/1/22.jpeg): failed to open stream: Permission denied in /opt/bitnami/apache2/htdocs/lookgram/build/classes/Photo.php on line 138
Warning: move_uploaded_file(): Unable to move '/tmp/phpAyWyw4' to '//home/bitnami/htdocs/lookgram/photos/1/22.jpeg' in /opt/bitnami/apache2/htdocs/lookgram/build/classes/Photo.php on line 138
This looks like a file permssion error, so here is the permissions on the folder:
drwxrwxr-x 6 bitnami bitnami 4096 Mar 15 01:02 photos
Any ideas?
This is because photos/1/ is only writable by root user. For upload to work we need to make the owner of that folder same as httpd process owner OR make them globally writable (bad practice).
Check apache process owner: $ps aux | grep httpd. The first column will be the owner typically it will be nobody
Change the owner of photos/1/ to be become nobody or whatever the owner you found in step 1.
$sudo chown nobody /home/bitnami/htdocs/lookgram/photos/1/
Chmod photo/1/ now to be writable by the owner, if needed [Seems you already have this in place].
$ sudo chmod -R 0755 /home/bitnami/htdocs/lookgram/photos/1/
For more details why this behavior happen, check the manual http://php.net/manual/en/ini.core.php#ini.upload-tmp-dir , note that it also talking about open_basedir directive.

Categories