PHP write outside document root - php

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.

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).

Can't require php files from another folder - permission denied

I have a web server where i host script1 in /home/srcipt1/ and script2 is in /home/script2
When i try to include a file from script2 folder into script1 i get permission errors
Warning: require_once(/home/script1/public_html/SSI.php): failed to open stream: Permission denied in /home/script2/public_html/q3/config/config.php on line 32
EDIT:
SSI.php has 755 permissions and /home/script2/public_html has 750 permissions
You may not have group permissions on that file. You could try this in your SSH shell:
chmod /home/script1/public_html/SSI.php 774
Make sure that you are using the correct ACL, 774 is group/user writable, readable and executable and world readable only.

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.

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.

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

Categories