blue elephant plush toy on black laptop computer

Set or remove the read-only attribute assigned to files with PHP chmod

Change file attributes with PHP chmod to make files read-only or normally accessible on Windows Server IIS.

Home » Codebase » Set or remove the read-only attribute assigned to files with PHP chmod

Chmod.php, change file attributes with PHP to make files read only or normally accessible on Windows IIS servers. Sometimes you need chmod to make files read-only on your website, or make them normally accessible in case they already are read-only. For instance Drupal’s settings.php configuration file, or WordPress Contact Form 7 temporary captcha files, are examples of read-only files.

Change file attributes with PHP chmod to make files read-only or normally accessible on Windows Server IIS.

PHP chmod function to make files read-only or normally accessible on Windows Server IIS

On Windows Server IIS web servers, the PHP chmod() function isn’t supported, because the Windows Server operating system doesn’t recognize chmod as a command. Except when changing file attributes…

Yes, you’ve read it correct; you can use PHP’s chmod() to change file attributes on Windows Server IIS web servers! For this to happen, files need to be writable for the user under which the website operates (IUSR on IIS).

Here is an example function to change file attributes with PHP. Save the code as chmod.php for example.

<?php 
/*
 * example PHP function to make files read-only
 * or normally accessible. Don't copy/paste this 
 * into production due to unsecured $_GET!
 *
 * Follow me on Twitter: @Jan_Reilink
 * Donate: https://www.paypal.me/jreilink
 */

  /* leave empty */
  $action = '';
  $file = '';

  if ( isset( $_GET['action'] ) && isset( $_GET['file'] ) ) {
    $action = $_GET['action'];
    $file = $_GET['file'];
  }

  switch($action) {
  case "remove_readonly":
    changepermission( $file, '0755' );
    break;
  case "add_readonly":
    changepermission( $file, '0600' );
    break;
  default:  // default action if no action is matched
    changepermission( $file, '0755' );
  }

  function changepermission( $files, $values ) {
    chmod( $files, $values );
  }
?>

As said, files need to be writable for PHP (e.g. the user under which the application pool runs) to be able to change file attributes.

Now call /chmod.php?action=add_readonly&file=myfile.php to make files read-only or /chmod.php?action=remove_readonly&file=myfile.php to remove the read-only flag using chmod.

Chmod.php, change file attributes with PHP to make files read-only or normally accessible on Windows IIS servers.

One-time donation

Please take a second to support Sysadmins of the North and donate, your generosity helps!

Summary

  • Use PHP chmod to change file attributes on Windows Server IIS; it can make files read-only or normally accessible.
  • Windows Server doesn’t support the PHP chmod() function directly, but it works for changing file attributes.
  • Ensure files are writable for the user under which the website operates (IUSR on IIS) to use chmod effectively.
  • Call /chmod.php?action=add_readonly&file=myfile.php to make a file read-only, or /chmod.php?action=remove_readonly&file=myfile.php to revert it.
Jan Reilink
Jan Reilink

In my day to day work, I’m a systems administrator – DevOps / SRE and applications manager at Embrace – The Human Cloud. At Embrace we develop, maintain and host social intranets for our clients. Provide digital services and make working more efficient within various sectors.

Read why we can use your help and support ❤️

Articles: 173