Skip to content

Instantly share code, notes, and snippets.

@matiferrigno
Last active December 16, 2021 01:48
Show Gist options
  • Save matiferrigno/47953387e1fff8c8b6a3dc23004e5ab0 to your computer and use it in GitHub Desktop.
Save matiferrigno/47953387e1fff8c8b6a3dc23004e5ab0 to your computer and use it in GitHub Desktop.
Check if volume is read-only and throw a http 500 response code.

Code

<?php
error_reporting(0);
try {
  $rc=200;
  if (!fopen("check_readonly.txt", "w+")) throw new Exception('File open failed');
  if (!fwrite($fp, "s")) throw new Exception('File write failed');
}
catch (Exception $e) {
  $rc=500;
}
finally{
  http_response_code($rc);
}
exit;

Test

RO:

$ docker run -d  -w /var/www/html -it -v $PWD/:/var/www/html:ro -p 9004:80 --rm php:8-apache 
$ curl -I localhost:9004/readonly.php                                                        
HTTP/1.1 500 Internal Server Error
Date: Thu, 16 Dec 2021 01:28:26 GMT
Server: Apache/2.4.51 (Debian)
X-Powered-By: PHP/8.1.0
Connection: close
Content-Type: text/html; charset=UTF-8

RW:

$ docker run -d  -w /var/www/html -it -v $PWD/:/var/www/html:rw -p 9005:80 --rm php:8-apache  
$ curl -I localhost:9005/readonly.php
HTTP/1.1 200 OK
Date: Thu, 16 Dec 2021 01:28:45 GMT
Server: Apache/2.4.51 (Debian)
X-Powered-By: PHP/8.1.0
Content-Type: text/html; charset=UTF-8
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment