Search code examples
phpfolder-permissions

Change folder permission to 777 using PHP temporarily


I have a Video folder on my server which has 755 permission. The problem is: when someone goes to upload video file, it can't be upload into that folder because of permission error.

If I change the permission to 777, then Video can be uploaded. But I don't want to allow the folder permission to 777 for security reason.

Is there any way in PHP to temporary change the permission to 777 while uploading video?


Solution

  • PHP provides a function, chmod() for the task.

    Attempts to change the mode of the specified file to that given in mode.

    You can put it in an if statement, and if it returns false, you can skip the upload file part.


    The usage will be like

    if( chmod($path, 0777) ) {
        // more code
        chmod($path, 0755);
    }
    else
        echo "Couldn't do it.";
    

    As described in the chmod function manual, the $mode must be in octal format - with leading zero, i.e chmod($path, 0777)