$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).
Related
I am trying to run a server on ArchLinux, I have apache and PHP running normally, but i can't upload a file with PHP. I have seen many questions of this sort on Stackoverflow and I seem to have used all the suggestions I found, but I still get a
failed to open stream: Permission denied in /srv/http/upload.php
error.
To be precise
Warning: move_uploaded_file(): Unable to move '/tmp/phpZvJK1l' to '/home/administrator/SCRIPTS/tr/solution.cpp' in /srv/http/upload.php on line 20
I set the permissions for /tmp/, /home/administrator/SCRIPTS/tr and /srv/http to 777 (I know thats not right, but I am working locally right now and I want to get it working somehow).
Also I set all the owner of these directories to http (that is the user running PHP), but the thing still doesnt work.
Some PHP configurations do not allow file access outside the users docroot directory, so you may not have access to /tmp from within PHP. Try uploading your file(s) to a temp directory within your /home/administrator directory - preferably to a directory that isn't accessible to web browsers (a sibling directory to your docroot).
give permission to the /tmp dir :
sudo chmod 777 /tmp
As an Arch user I have the same issue when I work on web projects.
I'll recommend you to see this part of the Arch Wiki
User directories are available by default through http://localhost/~yourusername/ and show the contents of ~/public_html (this can be changed in /etc/httpd/conf/extra/httpd-userdir.conf).
So do to so you have to create the ~/public_html directory then
You must make sure that your home directory permissions are set properly so that Apache can get there. Your home directory and ~/public_html must be executable for others ("rest of the world"):
$ chmod o+x ~
$ chmod o+x ~/public_html
$ chmod -R o+r ~/public_html
After that you don't need to put your file under /srv/http/ you can now use ~/public_html as development directory.
I'm trying to create a secure way to handle file uploads by uploading the files to a directory outside of public_html.
I'm testing my script and my script works when I execute sudo php receive_files.php, but when I run receive_files.php without sudo I get failed to open stream: Permission denied.
Right now all permissions all the way back to var/www/html/mysite/public_html are set to 755. I tried changing all of them to 775 and the command still didn't work
Without sudo. And I'm pretty sure that would not be secure. How do I get around this problem?
My code:
$encoded_file = $_POST['file'];
$user_id = $_POST['user_id'];
$decoded_file = base64_decode($encoded_file);
$new_directory = "../../../../../../user_uploads/$user_id/";
if(!file_exists($new_directory)){
mkdir($new_directory, 0775, true);
file_put_contents($new_directory . $decoded_file);
}
Sudo is required when you are not the owner of something or you try to change permissions. So if you want php script to have access to upper directory. Change the group or user owner of upper directory same as php executor, in most cases its www-data
You can check owner of php script by using
ls -al
Then change owner using
chown -hR www-data:www-data directorypath
For more detail check manual of chown command
This will most probably fix your issue
Additional info
specifying absolute path instead of relative path solved the issue
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?
When php-script is trying to write to a file:
-rwxr-xr-x. 1 eugene_val eugene_val 8033 Sep 10 10:47 ajax_EN.json
I get an error:
fopen(ajax_EN.json): failed to open stream: Permission denied
I wonder what could be an appropriate solution to it taking security into consideration.
The options I could think of are:
1) chown this file to apache user and chmod it to 700
2) add apache to a group of the file-owner
3) use suPHP and likes(which I would not like to because of the performance hit)
A better choice is to change the file's group to the Apache user group, and set the file to be group-writable:
$ chgrp <apache_group> ajax_EN.json
$ chmod g+w ajax_EN.json
The permissions of the path for this file also matter. Even if you chmod 777 the file, if the user trying to read it doesn't have read permissions for the path, they still won't be able to read the file.
When I use chmod() to change permissions at run time, it gives me the below message:
Warning: chmod() [function.chmod]: Operation not permitted in /home/loud/public_html/readalbum.php
How can I remove this error and make the chmod function work?
$ sudo chmod ...
You need to either be the owner of the file or be the superuser, i.e., user root. If you own the directory but not the file, you can copy the file, rm the original, then mv it back, and then you will be able to chown it.
The easy way to temporarily be root is to run the command via sudo. ($ man 8 sudo)
In order to perform chmod, you need to be owner of the file you are trying to modify, or the root user.
This is a tricky question.
There a set of problems about file permissions. If you can do this at the command line
$ sudo chown myaccount /path/to/file
then you have a standard permissions problem. Make sure you own the file and have permission to modify the directory.
If you cannnot get permissions, then you have probably mounted a FAT-32 filesystem. If you ls -l the file, and you find it is owned by root and a member of the "plugdev" group, then you are certain its the issue. FAT-32 permissions are set at the time of mounting, using the line of /etc/fstab file. You can set the uid/gid of all the files like this:
UUID=C14C-CE25 /big vfat utf8,umask=007,uid=1000,gid=1000 0 1
Also, note that the FAT-32 won't take symbolic links.
Wrote the whole thing up at http://www.charlesmerriam.com/blog/2009/12/operation-not-permitted-and-the-fat-32-system/
You, or most likely your sysadmin, will need to login as root and run the chown command:
http://www.computerhope.com/unix/uchown.htm
Through this command you will become the owner of the file.
Or, you can be a member of a group that owns this file and then you can use chmod.
But, talk with your sysadmin.