PHP : add new permission to a file












1















On my server I have some files with different permissions.



In my php script, I want to add writing permission to the group for each of these files.



Using linux cmd, chmod g+w file does the job.



But how to do that using php chmod() ?



This is what i tried so far:



$filePermission = fileperms('myFile.txt');
# g+w = 020
chmod('myFile.txt', $filePermission+20);


This gives me what i want, but is there a php alternative for linux chmod g+w ?










share|improve this question


















  • 1





    According to the documentation, you are not able to use g+w as a second variable to chmod function, only integers are allowed. There are two options: 1) use direct shell (creates new process, also you should escape file path); 2) use extra function to convert string to int - particular example can be found here php.net/manual/en/function.chmod.php#82751

    – user1597430
    Jan 2 at 17:11








  • 1





    Note that chmod takes octal values and 20 is not the same as 020.

    – Alex Howansky
    Jan 2 at 17:16
















1















On my server I have some files with different permissions.



In my php script, I want to add writing permission to the group for each of these files.



Using linux cmd, chmod g+w file does the job.



But how to do that using php chmod() ?



This is what i tried so far:



$filePermission = fileperms('myFile.txt');
# g+w = 020
chmod('myFile.txt', $filePermission+20);


This gives me what i want, but is there a php alternative for linux chmod g+w ?










share|improve this question


















  • 1





    According to the documentation, you are not able to use g+w as a second variable to chmod function, only integers are allowed. There are two options: 1) use direct shell (creates new process, also you should escape file path); 2) use extra function to convert string to int - particular example can be found here php.net/manual/en/function.chmod.php#82751

    – user1597430
    Jan 2 at 17:11








  • 1





    Note that chmod takes octal values and 20 is not the same as 020.

    – Alex Howansky
    Jan 2 at 17:16














1












1








1








On my server I have some files with different permissions.



In my php script, I want to add writing permission to the group for each of these files.



Using linux cmd, chmod g+w file does the job.



But how to do that using php chmod() ?



This is what i tried so far:



$filePermission = fileperms('myFile.txt');
# g+w = 020
chmod('myFile.txt', $filePermission+20);


This gives me what i want, but is there a php alternative for linux chmod g+w ?










share|improve this question














On my server I have some files with different permissions.



In my php script, I want to add writing permission to the group for each of these files.



Using linux cmd, chmod g+w file does the job.



But how to do that using php chmod() ?



This is what i tried so far:



$filePermission = fileperms('myFile.txt');
# g+w = 020
chmod('myFile.txt', $filePermission+20);


This gives me what i want, but is there a php alternative for linux chmod g+w ?







php chmod






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Jan 2 at 17:00









Hamza AbdaouiHamza Abdaoui

1,53721022




1,53721022








  • 1





    According to the documentation, you are not able to use g+w as a second variable to chmod function, only integers are allowed. There are two options: 1) use direct shell (creates new process, also you should escape file path); 2) use extra function to convert string to int - particular example can be found here php.net/manual/en/function.chmod.php#82751

    – user1597430
    Jan 2 at 17:11








  • 1





    Note that chmod takes octal values and 20 is not the same as 020.

    – Alex Howansky
    Jan 2 at 17:16














  • 1





    According to the documentation, you are not able to use g+w as a second variable to chmod function, only integers are allowed. There are two options: 1) use direct shell (creates new process, also you should escape file path); 2) use extra function to convert string to int - particular example can be found here php.net/manual/en/function.chmod.php#82751

    – user1597430
    Jan 2 at 17:11








  • 1





    Note that chmod takes octal values and 20 is not the same as 020.

    – Alex Howansky
    Jan 2 at 17:16








1




1





According to the documentation, you are not able to use g+w as a second variable to chmod function, only integers are allowed. There are two options: 1) use direct shell (creates new process, also you should escape file path); 2) use extra function to convert string to int - particular example can be found here php.net/manual/en/function.chmod.php#82751

– user1597430
Jan 2 at 17:11







According to the documentation, you are not able to use g+w as a second variable to chmod function, only integers are allowed. There are two options: 1) use direct shell (creates new process, also you should escape file path); 2) use extra function to convert string to int - particular example can be found here php.net/manual/en/function.chmod.php#82751

– user1597430
Jan 2 at 17:11






1




1





Note that chmod takes octal values and 20 is not the same as 020.

– Alex Howansky
Jan 2 at 17:16





Note that chmod takes octal values and 20 is not the same as 020.

– Alex Howansky
Jan 2 at 17:16












1 Answer
1






active

oldest

votes


















3














The syntax g+w is not valid for PHP, nor most programming languages as the underlying operating system has no concept of these values. The chmod syscall only accepts octal values, not strings. On your system the chmod command line executable has implemented a parser to implement this custom feature.



While your example "works", it will cause undesirable results if the file already has g+w, as adding another 20 will turn on additional bits and grant the file extra permissions you did not intend. For example, if the file permission was 020 and you added 020 to it, it would become 040.



Posix file permissions are octal bits where each digit represents a combination of read, write and execute. As such the add operation (+) is incorrect, you instead need to use bitwise operations such as AND and OR to turn individual bits on or off. For PHP, C, C++ and most common languages the AND operator is the ampersand symbol (&) and the OR operator is the pipe symbol (|). Since we wish to turn bits on, we need to use the OR operator.



The correct way to handle this would be:



$filePermission = fileperms('myFile.txt');
chmod('myFile.txt', $filePermission | 020);


Note that I have prefixed the number with a 0, this will make PHP interpret the number as octal rather then decimal.



Recently I made a video on this to try to help people understand chmod and how the octal notation works, it may help you with what you are attempting to accomplish. https://www.youtube.com/watch?v=j8FLsxdl0ns



If you wish to have the friendly menomic support as the command line binary has, you will need to implement the parser yourself, or invoke the command line binary from PHP, for example:



exec('/bin/chmod ' . escapeshellarg($perms) . ' ' . escapeshellarg($file));


Note however that this usage is far slower then using the inbuilt chmod function in PHP directly. It could also open you up to potential security issues.






share|improve this answer





















  • 1





    This is a very informative answer. Thank you sir!

    – Hamza Abdaoui
    Jan 3 at 8:03











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54010285%2fphp-add-new-permission-to-a-file%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























1 Answer
1






active

oldest

votes








1 Answer
1






active

oldest

votes









active

oldest

votes






active

oldest

votes









3














The syntax g+w is not valid for PHP, nor most programming languages as the underlying operating system has no concept of these values. The chmod syscall only accepts octal values, not strings. On your system the chmod command line executable has implemented a parser to implement this custom feature.



While your example "works", it will cause undesirable results if the file already has g+w, as adding another 20 will turn on additional bits and grant the file extra permissions you did not intend. For example, if the file permission was 020 and you added 020 to it, it would become 040.



Posix file permissions are octal bits where each digit represents a combination of read, write and execute. As such the add operation (+) is incorrect, you instead need to use bitwise operations such as AND and OR to turn individual bits on or off. For PHP, C, C++ and most common languages the AND operator is the ampersand symbol (&) and the OR operator is the pipe symbol (|). Since we wish to turn bits on, we need to use the OR operator.



The correct way to handle this would be:



$filePermission = fileperms('myFile.txt');
chmod('myFile.txt', $filePermission | 020);


Note that I have prefixed the number with a 0, this will make PHP interpret the number as octal rather then decimal.



Recently I made a video on this to try to help people understand chmod and how the octal notation works, it may help you with what you are attempting to accomplish. https://www.youtube.com/watch?v=j8FLsxdl0ns



If you wish to have the friendly menomic support as the command line binary has, you will need to implement the parser yourself, or invoke the command line binary from PHP, for example:



exec('/bin/chmod ' . escapeshellarg($perms) . ' ' . escapeshellarg($file));


Note however that this usage is far slower then using the inbuilt chmod function in PHP directly. It could also open you up to potential security issues.






share|improve this answer





















  • 1





    This is a very informative answer. Thank you sir!

    – Hamza Abdaoui
    Jan 3 at 8:03
















3














The syntax g+w is not valid for PHP, nor most programming languages as the underlying operating system has no concept of these values. The chmod syscall only accepts octal values, not strings. On your system the chmod command line executable has implemented a parser to implement this custom feature.



While your example "works", it will cause undesirable results if the file already has g+w, as adding another 20 will turn on additional bits and grant the file extra permissions you did not intend. For example, if the file permission was 020 and you added 020 to it, it would become 040.



Posix file permissions are octal bits where each digit represents a combination of read, write and execute. As such the add operation (+) is incorrect, you instead need to use bitwise operations such as AND and OR to turn individual bits on or off. For PHP, C, C++ and most common languages the AND operator is the ampersand symbol (&) and the OR operator is the pipe symbol (|). Since we wish to turn bits on, we need to use the OR operator.



The correct way to handle this would be:



$filePermission = fileperms('myFile.txt');
chmod('myFile.txt', $filePermission | 020);


Note that I have prefixed the number with a 0, this will make PHP interpret the number as octal rather then decimal.



Recently I made a video on this to try to help people understand chmod and how the octal notation works, it may help you with what you are attempting to accomplish. https://www.youtube.com/watch?v=j8FLsxdl0ns



If you wish to have the friendly menomic support as the command line binary has, you will need to implement the parser yourself, or invoke the command line binary from PHP, for example:



exec('/bin/chmod ' . escapeshellarg($perms) . ' ' . escapeshellarg($file));


Note however that this usage is far slower then using the inbuilt chmod function in PHP directly. It could also open you up to potential security issues.






share|improve this answer





















  • 1





    This is a very informative answer. Thank you sir!

    – Hamza Abdaoui
    Jan 3 at 8:03














3












3








3







The syntax g+w is not valid for PHP, nor most programming languages as the underlying operating system has no concept of these values. The chmod syscall only accepts octal values, not strings. On your system the chmod command line executable has implemented a parser to implement this custom feature.



While your example "works", it will cause undesirable results if the file already has g+w, as adding another 20 will turn on additional bits and grant the file extra permissions you did not intend. For example, if the file permission was 020 and you added 020 to it, it would become 040.



Posix file permissions are octal bits where each digit represents a combination of read, write and execute. As such the add operation (+) is incorrect, you instead need to use bitwise operations such as AND and OR to turn individual bits on or off. For PHP, C, C++ and most common languages the AND operator is the ampersand symbol (&) and the OR operator is the pipe symbol (|). Since we wish to turn bits on, we need to use the OR operator.



The correct way to handle this would be:



$filePermission = fileperms('myFile.txt');
chmod('myFile.txt', $filePermission | 020);


Note that I have prefixed the number with a 0, this will make PHP interpret the number as octal rather then decimal.



Recently I made a video on this to try to help people understand chmod and how the octal notation works, it may help you with what you are attempting to accomplish. https://www.youtube.com/watch?v=j8FLsxdl0ns



If you wish to have the friendly menomic support as the command line binary has, you will need to implement the parser yourself, or invoke the command line binary from PHP, for example:



exec('/bin/chmod ' . escapeshellarg($perms) . ' ' . escapeshellarg($file));


Note however that this usage is far slower then using the inbuilt chmod function in PHP directly. It could also open you up to potential security issues.






share|improve this answer















The syntax g+w is not valid for PHP, nor most programming languages as the underlying operating system has no concept of these values. The chmod syscall only accepts octal values, not strings. On your system the chmod command line executable has implemented a parser to implement this custom feature.



While your example "works", it will cause undesirable results if the file already has g+w, as adding another 20 will turn on additional bits and grant the file extra permissions you did not intend. For example, if the file permission was 020 and you added 020 to it, it would become 040.



Posix file permissions are octal bits where each digit represents a combination of read, write and execute. As such the add operation (+) is incorrect, you instead need to use bitwise operations such as AND and OR to turn individual bits on or off. For PHP, C, C++ and most common languages the AND operator is the ampersand symbol (&) and the OR operator is the pipe symbol (|). Since we wish to turn bits on, we need to use the OR operator.



The correct way to handle this would be:



$filePermission = fileperms('myFile.txt');
chmod('myFile.txt', $filePermission | 020);


Note that I have prefixed the number with a 0, this will make PHP interpret the number as octal rather then decimal.



Recently I made a video on this to try to help people understand chmod and how the octal notation works, it may help you with what you are attempting to accomplish. https://www.youtube.com/watch?v=j8FLsxdl0ns



If you wish to have the friendly menomic support as the command line binary has, you will need to implement the parser yourself, or invoke the command line binary from PHP, for example:



exec('/bin/chmod ' . escapeshellarg($perms) . ' ' . escapeshellarg($file));


Note however that this usage is far slower then using the inbuilt chmod function in PHP directly. It could also open you up to potential security issues.







share|improve this answer














share|improve this answer



share|improve this answer








edited Jan 4 at 1:11

























answered Jan 2 at 18:51









GeoffreyGeoffrey

6,67831938




6,67831938








  • 1





    This is a very informative answer. Thank you sir!

    – Hamza Abdaoui
    Jan 3 at 8:03














  • 1





    This is a very informative answer. Thank you sir!

    – Hamza Abdaoui
    Jan 3 at 8:03








1




1





This is a very informative answer. Thank you sir!

– Hamza Abdaoui
Jan 3 at 8:03





This is a very informative answer. Thank you sir!

– Hamza Abdaoui
Jan 3 at 8:03




















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54010285%2fphp-add-new-permission-to-a-file%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Monofisismo

Angular Downloading a file using contenturl with Basic Authentication

Olmecas