PHP & Permissions - php

I have an application that I am transferring to a new server. In doing so the upload feature for customers accounts stopped working because the owner of the folders was 'ftp' and not 'apache' I solved it by renaming the folder and then using a directory copy function that I copied and pasted from somewhere in to a new folder with the correct name and it was all cool after that.
My question is this, can php change the ownership of a folder or files?

Yeah, php can change ownership of a file. Use chown($file, $user). You could write a simple recursive script to change owner for each file using chown.

Yes, as far as the user under which it runs has the permissions to do that.
You could use the chown function or wrap the shell command in an exec call to do it recursively with no need to program that as in
exec('chown -R user <your-dir>');

Related

Deleting video files using unlink() in PHP 7

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.

Changing owner and group of newly created directories using PHP script

Dedicated Linux server running debain LAMP.
I run a PHP script (using a browser) which creates a directory (and various sub directories) in a folder on the same server for subsequent shared use using Dropbox.
The directories are created in /home/dropbox/New_Project_Name/new_folders and should be owned by the user 'dropbox'.
However running the php script causes the newly created directories generated by the script to be owned by 'www-data'
What is the best why of either running the php script from the browser so that it generates the new directories with ownership of user and group 'dropbox' or subsequently running a script to check for www-data ownership and recursively changing files and directories to 'dropbox'
Many thanks for any help.
Not tested, but after creating the folder, you can run another line of code to change the owner/group
// define user and group
$owner = "dropbox";
$group = "dropbox";
$folder = "/home/dropbox/New_Project_Name/new_folders";
// change the owner and group
chown($folder, $owner);
chgrp($folder, $group);
Keep in mind, that it might throw an error, because there are subfolders and the operation fails. A while loop should solve the problem.
There might be an issues with the permissions, up to the server-config
There is another way to run it recursively with the "exec" command.
you can go like this:
exec("chown -R ".$owner.":".$group." ".$folder);
This will change user and group for the folder and all sub-folders. But beware,
using system is "dangerous". You can run any shell-commands. Don't play around with it too much.
OK - finally got this working (thanks everybody) but adding the following to my /etc/sudoers
www-data ALL=(ALL) NOPASSWD: /bin/chown, /home/sites/public_html/change_owner.php
The contents of the PHP file were as in the answer from DasSaffe
Here are 3 options:
Use suEXEC.
Connect to localhost ftp as user dropbox and create directories and files this way.
Set up sudo so www-data user can execute this as root without password prompt: sudo chown -R dropbox /path/to/dir, then just use php's exec function.

Change ownership of folders created by PHP

I've just bought a VPS for testing purposes trying to learn how to use it etc.
I've setup apache/php and running a script which PHP creates folders. Ive tried setting the script to chmod it to 0755 but it still doesn't let me delete the folder.
I can't chown/chgrp as im not running the script as root.
I just need to be able to delete the folder with PHP, Is there a config file i can change so PHP creates folders with a different user group?
Thanks
Your folder is created by your script, that usually runs with the apache or web user. So you can't manipulate it as yourself (in an FTP for example).
What you can do is change the mode within your script (still running as apache or web user) like so:
<?php
chmod("/somedir/somefile", 0777); // octal; correct value of mode
?>

php created files ownership

I am having a irritating problem concerning the permissions of files created by Wordpress.
When i download plugins using wordpress or uploading images, and even when a php script creates a dir/files it puts the permissions of this folder into a different user/group.
My user does has no access to this file/folder under my own ftp account.
Is there a way to change files/folders ownership created by apache/php/wordpress to my user ?
If you are using Ubuntu (I don't know if int other distros the files are in the same location) you can edit the file envvars located in /etc/apache2 and restart Apache.
If you can use ACL this is a better solution than changing the user for Apache, more info: https://help.ubuntu.com/community/FilePermissionsACLs
If you want to change the permissions from a php script, you could use the chown() function

How to delete Folders created with PHP mkdir?

I have created folders using PHP's mkdir command.
Now I want to delete these folders over FTP or SSH.
I get the error "permission denied".
I am on a managed server so I do not have root access.
What can I do so I will be able to delete these folders?
Do I need to change the file permissions (chmod) using PHP?
The folders would have been created with the ownership/permissions of whatever account PHP was running under (Apache's, if you're doing this from a web-based script).
You wouldn't be able to chown the directories to another account, as that requires root permissions. You could have the script that creates the directories set them to mode 0777, which'd give everyone read/write/delete access to them, but you might not want to open up things that wide.
you have to change the permissions first:
chmod("/somedir/somefile", 755);
or whatever you like
then you can remove with
rmdir("dir")
Yes, you must run chmod after directory or file creation with PHP. Its because PHP runs with Apache permissions.
After chmod to PHP/Apache user you can rename, move or delete folders and files.
Check your permission first if you got any problem. Some folder you only can delete or chmod if you are owner.
If you are owner, then you can use PHP chmod.
CHMOD("PATH_TO_FOLDER",0755);
Then use unlink to delete files in folder:
unlink("PATH_TO_FOLDER/*.*");
And then
rmdir("PATH_TO_FOLDER")

Categories