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

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.

Related

Fedora PHP "failed to open stream: Permission denied"

So I've got Apache on web server running Fedora. I'm trying to write into the text file.
if(!empty($_POST['versionWrite'])){
$file = fopen(APP_DIR."/resources/version.txt", "w");
fwrite($file, $_POST['versionWrite'].PHP_EOL);
fclose($file);
}
And when I execute the code, I get this:
Warning: fopen(/var/www/spumprnagle/resources/version.txt): failed to open stream: Permission denied in /var/www/spumprnagle/head.php on line 28
Warning: fwrite() expects parameter 1 to be resource, boolean given in /var/www/spumprnagle/head.php on line 29
Warning: fclose() expects parameter 1 to be resource, boolean given in /var/www/spumprnagle/head.php on line 30
This happens in every script working with files. And I have no idea what shall I do to grant Apache permissions to edit files.
Thanks for your time :).
OK, I add this answer for who are searching for answer on Fedora. I changed file permissions to 777 and file ownership to apache:apache but it keeps throwing the same error. finally I found that in some versions of Fedora SElinux prevents Httpd from writing files. The solution is to disable SElinux as follow:
vim /etc/selinux/config
then edit the file as follow:
SELINUX=disabled
Then Restart.
It worked for me.
Happy coding.
Problem: The apache user doesn't have the permission to write file.
Solution:
chown -R apache:apache path/to/directory
where apache is the default user for fedora and path/to/directory is the path of the directory containing the files with you want to write.
If you want to give the permission to a single file then omit -R
I would start by checking permissions on the file /var/www/spumprnagle/resources/version.txt to ensure that it's writeable by the web user. Since you tagged fedora here, I believe that's usually the apache user, so try chown apache:apache /var/www/spumprnagle/resources/version.txt.
If you're unsure of permission setting you can always temporarily set 777, e.g. chmod 777 /var/www/spumprnagle/resources/version.txt. If that resolves the issue, you can adjust the permissions down to something more reasonable (644).

Permission denied with user ubuntu but not with user www-data on .csv file

I'm seeing weird Ubuntu permission issues only on a single .csv file when trying to access with php. The file is list.csv, owned by ubuntu:www-data, and ubuntu user is part of www-data group. The rest of the site works fine, no permission issues, but I see the following error when trying to load this file with php:
PHP Fatal error: Uncaught exception 'RuntimeException' with message 'SplFileObject::__construct(xxx/list.csv): failed to open stream: Permission denied'
If i do chown www-data:www-data list.csv, i can load with php. Why is this file not being loaded by php if owned by ubuntu, even if ubuntu is part of www-data group?
Additionally, I can open this file as ubuntu user (vi list.csv) read and write.
Edit: Permissions on the file are: -rwxr-xr-x 1 ubuntu www-data
Edit again: Changed permissions to 0644 -rw-r--r-- 1 ubuntu www-data no luck
Edit some more: File lives in a directory that has x permission:
drwxr-xr-x 3 ubuntu www-data 4096 Jul 28 23:09 content/
Not sure it has anything to do with execute permissions as I can change owner to www-data:www-data and the file gets loaded. It would seem its something to do with user ubuntu owning the file and not www-data
Edit: Its this line of code that errors out, trying to load .csv file into php SplFileObject:
SplFileObject::__construct("xxx/content/list.csv");
I can see in the OP that the only difference between the owner and group is write permission (you have tried xr and r with the group). So, I suspect your PHP is trying to open the file in write mode.
To confirm this try:
$myCSV = new SplFileInfo('YOUR CSV');
$myCSV->isReadable(); // should be true
$myCSV->isWritable(); //expecting this to be false
Edit: If that's whats going with your program you can open the file just in read mode using: $myCSV->openFile('r')
Your server might have the PHP open_basedir parameter set which would prevent access to any files outside of this directory.
To verify this have a look into your relevant php.ini file (for example /etc/php5/apache2/php.ini) and check if the open_basedir line is set.

Warning: require(file): failed to open stream

I've changed development machines and have moved across one of my projects. However, when I try to run one of the files in this project, I get the following error message:
Warning: require(/var/www/libraries/facebook.php): failed to open stream: Permission denied in /var/www/logout.php on line 11 Fatal error: require(): Failed opening required '/var/www/libraries/facebook.php' (include_path='.:/usr/share/php:/usr/share/pear') in /var/www/logout.php on line 11
Anyone know why this would be happening?
It might be that I have the permissions set incorrectly. What should the file and folder permissions be?
All files are currently set to -rw-r--r-- and folders are set to drwx------, is that right?
Sometimes when things get moved paths can change. The error indicates that the file '/var/www/libraries/facebook.php' could not be found. Make sure that the file exists in that location.
Sometimes the web server runs with different credentials than the user. Try setting the directories to drwxr-x---, and the files to -rw-r--r--. If that doesn't work, then try changing the directories to drwxr-xr-x.
You are getting a Permission denied error which means that your libraries folder can only be accessed by the root user. Try to execute the script as a root user or give read permissions to the folder.
## use only for files ##
chmod 0444 /var/www/*
chmod 0444 /var/www/*.php
TO set directories in read-only mode, enter:
## use only for dirs ##
chmod 0544 /var/www/
chmod 0444 /path/to/your/dir/
You say the permissions are drwx------. Try changing to the /var/www/libraries directory in the shell as a non-root user. If you get an error there, the web server will also. You might need 755 permissions so the server can traverse to that directory. Just be careful what else is in that folder since it could be readable by the whole world.

Permission denied when opening or creating files with PHP

I can't create files with php, because the file dosent got permission for that.
I get this error:
Warning: fopen(test.txt): failed to open stream: Permission denied in /web/com/example.com/index.php on line 20
Warning: fwrite() expects parameter 1 to be resource, boolean given in /web/com/example.com/index.php on line 21
Warning: fclose() expects parameter 1 to be resource, boolean given in /web/com/example.com/index.php on line 22
This is the code I was using:
<?php
$file = fopen("test.txt","w");
echo fwrite($file,"Hello World. Testing!");
fclose($file);
?>
Simple as that! This is example code from w3schools.
So I need help to find out how to give the file the needed permissions.
I'm uploading files to my site with the net2ftp FTP web application, if it matters.
The folder your PHP script is trying to write to will probably be owned by the root user. Your PHP script is more than likely running under the www-data user if you're using a default Ubuntu/Apache/PHP setup.
As such you need to:
chown -R www-data:www-data folder
chmod -R g+w folder
If you find PHP is running under a user that is different from www-data then just change the user a group in the first line of code.
PS. change "folder" for your actual folder name.
The user running PHP (usually the apache user) doesn't have write permission on the folder the script is running in. Try using an absolute path, like "/tmp/test.txt" -- tmp is usually writable by any user, but the contents tend to be wiped out on reboot.
I am using Ubuntu and i solved it by running the command below in one directory up from the folder(.i.e uploads) i was writing the files in.
sudo chmod 777 ./uploads

PHP write outside document root

I'm trying to create a file in a directory outside the document root of the web server. The folder has permissions 777 but php says Permission Denied:
Warning: fopen(/home/site2/public_html/images/x.jpg) [function.fopen]: failed to open stream: Permission denied in /home/site1/public_html/test_sites.php on line 2
Permission problem
<?php
$f = fopen('/home/site2/public_html/images/x.jpg', 'wb');
if(!$f) die("Permission problem");
fclose($f);
echo "OK";
?>
The 777 permissions is half the battle. You will also need to change the group to be www-data (if on debian) using the:
chgrp g+w www-data /home/growtent/public_html/images
That "should" work, depending on what system you are running and given that my memory is correct.
It sounds like safe mode is on and doc_root has been set. You'll need to set doc_root to empty or turn off safe mode.

Categories