Folder and file sharing in Drupal
You wouldn't actually need to write any custom code to implement something like this.

A possible solution is outlined below. It is aimed at Drupal 7. Some of the functionality mentioned may not be ready for Drupal 8 so you might need alternate modules or custom code in those cases.


Create a new content type, which would be named "Exam paper" or similar. There would be one node per student per exam.
Add a multiple value file field to the content type. Make sure it uses the private file system. Using the private file system will mean that only users with access to the node will be able to access the files attached to the node.
You can use the file field paths and token modules to make it so that the file directory structure and file names match the pattern you want. With the tokens you can easily reference student, teacher or exam information from fields on the node in the path or file name. This has no effect on access control but might make things better for you in other ways.
There are a number of ways you can handle access control as there are a variety of access control modules available for nodes, however I would be inclined to go with the node access user reference module. So you would add an entity reference field on the content type that references users with the student role. You can then set permissions so that the referenced user can access the node and other users cannot.
You can use the rules module to notify the user under various circumstances.
You can use Drupal core comments to have comments on exam paper nodes.
Due to using nodes you can leverage the wide variety of add on modules that focus primarily on nodes.
If you want to store additional data with each specific file, you can install the file entity module, which will allow you to add fields at the file level as well as the node level.