Why does PHP give Permission Denied errors when writing file? - php

I'm running XAMPP 1.8.2 with the default PHP 5.4.19 on OS X 10.8.5.
My PHP application is supposed to have read/write access to a certain local directory, but it's not working. For testing, I distilled it down to a very simple script:
<?php
$file = "/Volumes/RAID/AT_RISK/copra/uploadedfiles/file.txt";
$current = "hello world\n";
file_put_contents($file, $current);
?>
Warning: file_put_contents(/Volumes/RAID/AT_RISK/copra/uploadedfiles/file.txt): failed to open stream: Permission denied in /Applications/XAMPP-1.8.2/xamppfiles/htdocs/filetest.php on line 4
I've tried this command, but it doesn't help.
sudo chmod -R 777 /Volumes/RAID/AT_RISK/copra/
Here you can see the permissions for yourself:
$ pwd
/Volumes/RAID/AT_RISK/copra/uploadedfiles
$ ls -l
total 32
-rwxrwxrwx 1 elliott staff 7 Oct 12 22:31 file.txt

Silly me. Although the folder had correct permissions, the volume itself (/Volumes/RAID) was 700. Problem solved.

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.

PHP No permissions to create file/folder

I got an issue with my centos server, It, from 3 days ago won't allow me to edit files (write into .txt) create files or create directories with mkdir..
I checked the file ownage, runned whoami and checked the php file had the right permissions but it doesn't want to work.
Might someone suggest me troubleshootings? Also I tried switching from nginx to httpd and repeat the permission checks but still nothing.
running ls -l on the php script it outputs the following:
-rwxrwxr--. 1 nginx nginx 63 Dec 21 14:31 cartella.php
and after : chown apache:apache -R /usr/share/nginx/html/cartella.php
-rwxrwxr--. 1 apache apache 63 Dec 21 14:31 cartella.php
also permissions I tried setting them to 644/755/777 but neither one worked.
Php code that worked till a few days ago:
exec(" > test.txt"); //to empty the file and set the right permissions
$myfile = fopen("test.txt", "a") or die("Unable to open file!");
fwrite($myfile, $text);
fclose($myfile);

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.

fopen() fails to open stream: permission denied, yet permissions should be valid

So, I have this error:
Warning: fopen(/path/to/test-in.txt) [function.fopen]: failed to open stream: Permission denied
Performing ls -l in the directory where test-in.txt is produces the following output:
-rw-r--r-- 1 $USER $USER 1921 Sep 6 20:09 test-in.txt
-rw-r--r-- 1 $USER $USER 0 Sep 6 20:08 test-out.txt
In order to get past this, I decided to perform the following:
chgrp -R www-data /path/to/php/webroot
And then did:
chmod g+rw /path/to/php/webroot
Yet, I still get this error when I run my php5 script to open the file. Why is this happening? I've tried this using LAMP as well as cherokee through CGI, so it can't be this.
Is there a solution of some sort?
Edit
I'll also add that I'm just developing via localhost right now.
Update - PHP fopen() line
$fullpath = $this->fileRoot . $this->fileInData['fileName'];
$file_ptr = fopen( $fullpath, 'r+' );
I should also mention I'd like to stick with Cherokee if possible. What's this deal about setting file permissions for Apache/Cherokee?
Check if the user that PHP runs under have "X" permission on every directory of the file path.
It will need it to access the file
If your file is: /path/to/test-in.txt
You should have X permission on:
/path
/path/to
and read permission on /path/to/test-in.txt
Another reason of this error may be that the directory of file does not exist.
I just add php code before fopen:
if(!file_exists(dirname($file)))
mkdir(dirname($file));
This help me out.

Categories