Can someone please explain best practice for permissions with PHP.
What case should something be writable, executable, what owner is best, etc. ?
Using an apache server
|
Can someone please explain best practice for permissions with PHP. Using an apache server |
|||||||||
|
|
Commonly used permissions for Cpanel are:
|
|||||||
|
|
0655 is the best permission level. There's really no reason for changing your files above this. Of course there may be a folder here or there that requires some write permissions, but for everything else 0655 will work. As a tip, make sure that all of your files are owned by apache. This can easily get changed if you ftp'd your files onto the server. So make sure you set them to apache for production sites. This will eliminate a lot of permission issues when running such a tight ship. |
|||||
|
|
PHP scripts should be editable by the owner, readable by a group the apache user is in, and optionally readable by anyone. They don't need to be executable. Ideally, most of the php scripts should be outside of a web-accessible folder, especially any configuration files. This way even if there is a problem with the apache configuration, your php files will never be exposed to the web. Often you'll just have an index.php page which calls require_once() on a script in the protected directory outside the web-accessible folder. A .htaccess file rewrites all incoming requests so that they go through the index.php, which then uses the router in the protected directory to figure out what to serve. |
|||||||||||
|
|
Basically most functions / methods attempting to write to a file in PHP should have an idea like this:
Is a good idea to make sure that you can access the file before attempting to do it because it's always better to show the user a custom error like "There was a problem" than showing a classic PHP error like "Unable to write to file..." or something... Hope I can help! |
|||
|
|
|
Try to use chmod function http://ua2.php.net/manual/en/function.chmod.php |
|||||
|
|
You may be confusing the roles of PHP and the file system. PHP does not have read, write, or executable permissions. Those are handled by the underlying filesystem (ext4, NTFS, etc). You can use PHP functions such as is_writable() and is_readable() to determine the permissions of a given file, and chmod() to change them. |
|||
|
|