I have a php file that when ran, it creates another file. Should the php file that creates the file have 750 permissions to write the file or should the dir that the file is being written to have the 750 permission?
First, the permissions of the php file have nothing to do with that.
Assuming that the user who executes the script is the owner of the directory the permissions of that directory should be set to
0700
or
0750
if members of the group that owns the directory should already have write and listing permissions. But I cannot give you the exact permissions for your use case. Therefore I need to know what is the exact scenario
There is a rule of thumb in the permission. Maximum permissions are Files 0644 and Folders 0750.
If you need to give any permission simply chmod it just before the action and then change it to the above rules.
Hope it help.
Related
My site is on a shared hosting. I've been using FTP and PHP File Manager to upload and delete files. Recently I've found a file I couldn't delete due to permissions, neither in PHPFM nor in FTP. So I've used DirectAdmin (the only option for my plan) to reset all permissions. Then I set all permissions for all files in public_html and subfolders to 777 recursively. I know only that it allows me to do more than any other permission variant.
Now the site is running in a static way, I can open PHP File Manager and it has no permission to upload files. I can upload files in DirectAdmin, however, but that feels unsafe. PHP File Manager reads: 'I/O error'. The directories look like this:
What do 1422 and 1420 mean? What can I do to upload files again? Thank you for the help.
1422 is the user_id the file belongs to and 1420 the group_id it belongs to.
Linux has a permission system, where you can give special permissions to the owner, your group and everyone else.
Permission 777 means everybody can read/write/execute, your group (1420) can read/write/execute and you (user 1422) can read/write/execute the file.
Permissions:
1 is execute file or open directory
2 is write
4 is read
Read/write permission is 2+4=6, read/execute (or open a directory) is 1+4=5
The three numbers represents [owner][group][everybody], so setting a file to 644 means user can read/write and everybody else just read a file.
Edit: The safe thing is to set all files to 644 and directories to 755. Private files should be 600 and executable files 755 (PHP files are NOT executable).
Apache is run as user apache or httpd, which is another user, therefore you must give "everybody" permission to read your PHP files and directories.
Edit2: If you need PHP to upload files, it is really done as user apache/httpd. Therefore you need to give full privileges to "everybody" to open directories and read/write 777. The file permissions should be 666.
I need to change the permissions and owner / group on a series of files within a folder on an Apache server w/PHP 4. My script will change the info if it is of type .txt (text file). But will not chmod, chown, chgrp for files with an extension of .incl (include) files which are copied into HTML/PHP files via the include or require function.
Can someone help me understand this. The folder is a 0777, the incl files are 0644 with owner glnorg / group users (created by my filezilla login).
I need to change to the following: owner nobody / group nogroup so the user can log in to their website and edit under their login (albeit generic).
Otherwise the editor, when saving, gives a permission warning message (annoying).
You can use chmod 777 *.txt
What it will do is assign full read and write permission to all txt files in the folder for all users and group.
However you can change the permission from 777 as per your need.
I have an uploads directory on my server, php upload works fine when folder's permission is set to 755 but if I make new dir inside uploads (uploads/subuploaddir) upload fails and I need to set 755 permission for this folder manually, so my question is how can I set 755 permission for uploads dir and all of it's future created sub directories?
PS. chmod -R 755 ...mypath/uploads doesn't work
Thanks in advance.
All answers are very appreciated.
Peter
UPDATE:
Problem solved, my fault. PHP creates folders with permission 777 as default and I created folders for testing purposes through FTP manually, so it was created by another system user and with different permission.
Use
umask(022);
in PHP before creating a file (e.g. line 1 in index.php). Using that option, every files that PHP creates will have a permission that equals to 755. If that doesn't work, search your code for calls to umask and change them to the one above.
you can use this command
chmod -R 755 directorypath or file
I'm using ZipArchive to create Zips and then provide them to download. I save them in a folder and I want to delete all every night (my cronjob would do that).
But now I've seen that they are created using CHMOD 644, and if I try to delete them with my script, I always can't delete them because I do not have sufficient rights.
How can I declare that every new Zip which is created uses 777?
Thanks for help! Flo
Are you able to chmod it like so?
chmod("/somedir/somefile", 0777);
The ability to delete files from a directory depends on the permissions of the directory, not the individual files inside. If your directory is 0777, you'll be able to delete the files regardless of their permissions. On the other hand, even if your files are 0777, you might not be able to delete them if your directory is unwritable to your cronjob.
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")