How to create or delete a file in the temporary directory

See the \Magento\Shipping\Model\Shipping\LabelGenerator::createPdfPageFromImageString()
method as a real usage example:

Step 1

Getting a temporary directory writer:

Step 2

Creating a temporary directory, if it is absent.
In most of the case this step is not needed because if you write a file with the Magento 2 methods then the directory will be created automatically.
But the \Magento\Shipping\Model\Shipping\LabelGenerator::createPdfPageFromImageString() method writes a file in non-standard way by using the imagepng function which does not created the file path automatically.

Step 3

Write a file. This is a non-standard way:

The standard way is described in the article How to write a file programmatically.

Step 4

Deleting the temporary file:

See also other filesystem topics: