I'm stuck with a problem that I thought was easy. I have to temporarily rename a bunch of files. All folders are 0755, files are 0644. The server runs PHP 5.5 in CGI Mode. For test purposes I put both the script and the file to rename in the same folder:
$root = $_SERVER['DOCUMENT_ROOT'];
chmod ($root."bla/_bla.php", 0777);
rename($root.'bla/_bla.php',$root.'bla.php');
chmod ($root."bla/bla.php", 0644);
But all I get is the "permission denied" error. Owner and group are for all files and folders the same. No luck even if I change folder and file to 0777. What exactly am I missing here?
Thanks in advance for any help. Hope, this isn't a duplicate, but I couldn't find an answer here.
Try to chmod your php script. I meant this script not the one you want to rename. If you are using linux goto terminal and chmod. I think this would work
Related
I'm facing a problem with deleting video files from folder using php unlink() function , image are deleting but when trying deleting videos it says
unlink(file_path) : permission denied.
You (running your script as either through CLI or a webserver) need write access to the directory in which the files are located. so access to the file is not enough.
Your image directory would be different and writable for webserver or cli.
chmod("your/video/dir/path",0777);
try using above code before unlink the video in your script.
Edit: Looks like you are using Windows. Unfortunately, my answer is for Unix-like operating systems (so Linux, MacOS). You could try installing Bash extension for Win8+, but still I'm not sure if this would work. However, I'm keeping my answer in case of anyone with similar problem is looking here for an answer.
Changing permissions through PHP might work in some cases, but not always, because if you don't have permissions to delete the file you might also not have permissions to change them.
The best solution is to create a directory where you will keep files to which PHP will have full access. Let's call it dirname. Once you have created a directory, change its owner and group to the one corresponding to your web server user's name (if you're using Apache, it's "www-data"), for example: chown www-data:www-data dirname.
Once you've done that, change folder's permissions. My suggestion is 744, it will assure that user who owns it will have all permissions, and everyone else will be able only to read it. To do that, execute the following command: chmod -R 777 dirname.
Now you should be able to do anything you want with the files in given directory directly from PHP.
Accidentally i rewrote all folders permission from root.
chown -R www-data:www-data /
Just for example. Luckily i have another server. So i started fix all permissions manually one by one. And now everything seems work fine except for one thing: php can't write files.
I have a suggestion that some php or apache process have wrong permissions.
So symptoms:
The stream or file "/var/www/vhosts/.../httpdocs/storage/logs/laravel.log" could not be opened: failed to open stream: Permission denied
Yes. I'm sure that files have correct permissions. Because this recursive process was not so fast to override /var folder too. At least some domain still untouched. So i checked it out.
Maybe it would be helpful to know that i use plesk. Because some .sock files could be located there.
I have no idea what i did but it works now. I hope one day it could be useful for someone too.
Just run script if you use plesk
/usr/lib/plesk-9.0/install_suexec
So in the we could say i just reinstall script.
I have the following directories:
/var/www/temp
/var/www/users (S3 mount)
the user which the following php is run on is www-data:
mkdir("temp/id247439757");
addSomeFilesInTheAboveDirectory();
shell_exec("temp/id247439757 users/id247439757");
the problem is that it's not moving the directory from temp/ to users/! All the files stay in the temp directory.
I think the user that executes shell_exec is www-data as well! how can I fix this? Please note that this problem cannot be addressed to the fact that it's a mounted directory as, if I directory do mkdir("users/id247439757") it does work.
You forgot the "mv" command inside the shell_exec call:
shell_exec("mv temp/id247439757 users/id247439757");
Help! PHP cannot write to any files in my web directory. I don't know why! I have the permissions of the file set to 777, but it is not working! Here is the code in question:
<?php
if ($f=fopen('test.txt', 'a'))
echo 'file opened';
fclose($f);
Nothing is being echoed! I don't know why :(.. the userid and gid is 0:0 from the script, and if I try to chown to that it doesn't work.
Please help I need this fixed asap this should be an easy thing to do but the damn server is being difficult.
And its running on Cent OS if thats any help..
You'll need to enable the appropriate SELinux booleans and label the directory with the appropriate file context if you want to write files there.
Or you could disable SELinux. But don't do that.
try to use absolute path. might be other dir then you think. also might be network file system that cannot be accessed as root.
If you're just uploading this php to a typical multi user server and you have 777 permissions but cannot write files, the most likely "culprit" is suPHP.
In a system with the suPHP module installed, one cannot have 777 permissions.
You can check for the existence of this installed on your system with phpinfo().
How should I handle image uploading using PHP?
How should I handle the chmod settings?
Example;
I have a dir called /image/ where i want to upload all my images.
Should I set this dir to chmod 777 and leave it like that? Or should i change chmod on that folder via PHP each time I need to upload a image. Is this correct, or should I be doing something else?
As thephpdeveloper mentioned, setting chmod once is enough. All subsequent writes into that directory will not change the directory permissions unless you explicitly chmod it to another permissions somewhere else.
The recommended permissions for directories on a *nix server is 755.
Setting permissions to 777 is not recommended. As mentioned by wic, it gives full permissions to everyone that have access to your server. Which makes it vulnerable if you are on shared hosting or sharing the server with other users.
Also to note is how PHP is run on your server. In fact, if you are running PHP as cgi, example suphp, permissions of 777 for directories are not allowed. Having 777 permissions on the directories your scripts reside in will not run and will instead cause a "500 internal server error" when attempting to execute them.
I recomend chmoding to 755
Only the user running the web server dameon needs permissions to the directory for writing. And you certainly don't want execute permissions on a directory users are uploading to.
Usually, folder settings are set once and that's it. It's rather pointless to keep setting the folder permissions to 777 via PHP, when you have already set it to 777.
No, you dont have to change the permissions on the directory each time. Once set, they are set so to speak.
Using 777 is overkill since it gives full permissions to everyone. Remove the 'x' bit and let apache (or whoever) own the directory. This makes it impossible to list files.