Php fails to create directory while all permissions should be set: - php

I've read all the relevant topics and went trough the anwers,
but my program is still not workint properly.
I get the following error message:
Warning: mkdir(): Permission denied in /var/www/printing/uploader_temp.php on line 28
Where line 28 looks like this:
mkdir("upload", 0777, true);
The current user who is logged in is called "server".
The owner of both var/www/ and var/www/printing is "server". (without " of course)
The attribute of var/www/ptinting is -777.
I modified the following rows in /etc/apache2/envvars to look like this:
export APACHE_RUN_USER=server
export APACHE_RUN_GROUP=server
and restarted apache server.
I can create folders manually on server or using FTP.
Does anyone have an idea what I did wrong?
Where else should I set permissions?
The OS is Ubuntu.
Thank you for your answers.

Did you tried to give permissions to apache user? I had this problem on my web application and I solved it in two commands:
chown -R www-data:www-data /path/to/webserver/www
chmod -R g+rw /path/to/webserver/www
Hope this helps for you too.

Try to have a look to AppArmor:
https://wiki.ubuntu.com/AppArmor
Try to create a file or a directory in the /tmp/, can you?

Related

PHP mkdir() is not working in browser [duplicate]

This question already has answers here:
PHP mkdir: Permission denied problem
(15 answers)
Closed 6 years ago.
I'm trying to create directory on my local apache server with php.
I tried
<?php mkdir("folder"); ?>
and
<?php exec("sudo mkdir folder"); ?>
When I try to execute them in browser nothing happens.
But I can execute them from terminal by using sudo. (I also modified sudoers file so it won't prompt for password in second code)
When I don't use sudo I get this error
PHP Warning: mkdir(): Permission denied in /var/www/html/mscr/add.php on line 2
I've also tried this and this .
So I can execute almost everything but directory operations in browser.
I want to be able to create, delete and edit directories in browser.
Thank you!
Sounds like Apache doesn't have permission to create the folder. Either give Apache ownership of the folder (where you're trying to create a new one)
sudo chown www-data /var/www/html/mscr
or set the folder permissions to 777
sudo chmod 777 /var/www/html/mscr
Warning this will make everything within the mscr folder executable. To get around this most CMS will create a subfolder which is set to 777 so something like /var/www/html/mscr/uploads
<?php mkdir("folder", 777, true); ?>

Permission Issues

Having an issue with unlink() even when executing a script as root.
Getting this error:
Warning: unlink(/var/www/html/services/training/add.php): Permission denied in /var/www/html/sites/services/functions.php on line 228
The owner of the file is root and the file permissions are 775 so it should work.
Are there any further steps I can take to troubleshoot this? Not sure where to go from here...
Open your terminal .. I guess you are an ubuntu user
so just do this
sudo chmod 777 -R /var/www/html/services/
By Default your access is limited . So you have to extend your access using this chmod command :) .. that's it :)

can't set the proper file permission for php file upload

I know that there are a lot of similar questions, but just hear me out and if this was a silly question and there was nothing new about it, i'll just delete this question.
I'm trying to upload using php, but I get permission denied. So here's what I did to solve it and didn't work:
first of all, this is localhost, so i can't set the owner of the files to apache, because I won't be able to edit them.
So I got the apache user name in php, and it was www-data
I created a new group called 'localhostowners'
I added my own user to this group
I added www-data to the group
when I run
grep -i 'localhostowners' /etc/group
I get
localhostowners:x:1001:mnvoh,www-data
And then I set the file permissions for everything on the localhost dir to 764
and set the owner with this:
sudo chown -R mnvoh:localhostowners localhostdir
But now I can't execute the php scripts hence the 4. although according to what I know, the 6 should be affecting apache, since I set the group on the files. What am I doing wrong here???
Thanks everybody :)
Test with:
sudo chown -R mnvoh:localhostowners localhostdir
sudo chmod 764 -R localhostdir
-R: recursivity
First you should check the permission the file
ls -a
or
ll
and make sure the person who want to execute the file have X permission.
-user-group-other
-rwx rwx rwx

fopen() Permission Denied despite having correct file directory

$handle = fopen('/Applications/XAMPP/xamppfiles/htdocs/test/file.txt', 'w');
I tried doing the above and every time I try it, the following statement appeared on my browser:
Warning:fopen(/Applications/XAMPP/xamppfiles/htdocs/test/file.txt)
[function.fopen]: failed to open stream: Permission denied in
/Applications/XAMPP/xamppfiles/htdocs/test/index.php on line 26.
I tried looking through answered questions with the same type of questions but most of the things I tried did not work. For example, writing the full directory...
Maybe, you have no premissions to acces the file. One of the answet, is that, you must change CHMOD to e.g. 777. You can co it with your ftp explorer or with PHP.
chmod("/somedir/somefile", 777);
By default when XAMPP is installed, the htdocs folder does not have any read permissions. You can change the permissions through the terminal like this.
cd /Application/XAMPP/xamppfiles/htdocs/test/
sudo chmod 0664 file.txt
Alternatively, you can recursively set all the permission level of all files and folders
cd /Application/XAMPP/xamppfiles/
sudo chmod -R 0664 htdocs/
You could chmod to 777, but that is risky security. What I'm guessing you really want is change ownership of the file. You can do this using chown. PHP usually runs as user www-data, so you'd run a command something like this.
sudo chown www-data:root path/to/file.ext
If you're file permission on the file was something normal like 664, that'd give PHP the 6 permission (Read and Write) instead of the 4 (just Read).

PHP Write Permission - FC13

I have recently installed FC13 and am attempting to write a mechanism in my PHP code that caches gathered data into a specific directory (for our purposes here, let's call it /var/www/html/_php_resources/cache).
I copy my files over to the /var/www/html directory and then run chown -R apache:apache /var/www/html/* and chmod a+w /var/www/html/_php_resources/cache on the new data. For right now I am just using the global write permission for convenience. I will tweak the permissions later.
When I attempt to use the chmod or mkdir PHP functions I wind up with:
Warning: chmod(): Permission denied in /var/www/html/_include/php/CacheInit.php
or
Warning: mkdir(): Permission denied in /var/www/html/_include/php/CacheInit.php
Now, when I disable SELinux everything works just fine. The problem is that I would prefer not to disable SELinux and actually get the permissions set up correctly so that I can port it over to servers where someone does not have such explicit control.
As an example: my personal site host allows me to set read/write permissions on directories but will not allow for SELinux policy changes.
FYI:
uname -r = 2.6.34.7-56.fc13
*php -version * = PHP 5.3.3
rpm -qa | grep httpd = httpd-2.2.16-1.fc13
Does anyone have any suggestions?
I had the same problem, trying to mkdir from php. Not so much information on google but this is what I found and I guess this is the correct solution. One have to label the dir in which apache should create directories.
Label should be "httpd_sys_script_rw_t" and I found that info here: http://docs.fedoraproject.org/en-US/Fedora_Core/5/html/SELinux_FAQ/index.html#id672528
Here's how to label the dir: chcon -R -t httpd_sys_script_rw_t <dir>
Reference somewhere here: http://www.centos.org/docs/5/html/Deployment_Guide-en-US/rhlcommon-chapter-0017.html
Hope this help someone out there.

Categories