Tutorials, PHP & MySQL

Change file permissions dynamically with php

A tutorial to change file permissions dynamically with php

First define the function name in this case call it change Permissions with two parameters $path is the path to the folder and $modlevel is the permissions level to use.

function changePermissions($path,$modlevel){

Then using the command chmod we change the file permissions.

chmod($path,$modlevel);

Then do an if statement to see if it's worked or failed this is optional

if(chmod){
  echo "success";
} else {
  echo "failed";
}

To run the function you need to call it, you call it by typing it's name with the parameters

changePermissions("/assets/images/uploads/",777);

Here's the full script:

<?php
function changePermissions($path,$modlevel){
chmod($path,$modlevel);
if(chmod){
  echo "success";
} else {
  echo "failed";
}
}

changePermissions("/assets/images/uploads/",777);
?>

That's it now you should be able to change file permissions dynamically with php.

David Carr

David Carr

For the past 12 years, I’ve been developing applications for the web using mostly PHP. I do this for a living and love what I do as every day there is something new and exciting to learn.

In my spare time, the web development community is a big part of my life. Whether managing online programming groups and blogs or attending a conference, I find keeping involved helps me stay up to date. This is also my chance to give back to the community that helped me get started, a place I am proud to be apart of.

Besides programming I love spending time with friends and family and can often be found together going out catching the latest movie, staying in playing games on the sofa or planning a trip to someplace I’ve never been before.