Create a temporary directory using Javascript, Typescript, or Php

When developing an application that interacts with the file system it is usually necessary to perform operations with files before storing them permanently or simply store short-lived data which may not be necessary after it's been used. A common practice is to create a temporary folder where we can store the data without caring about it from being deleted afterward. The best approach is to let the operating system deal with it by storing the data inside its appropriate temporary folders location.

The TurboDepot library FilesManager class will help us to deal with this process easily, via the createTempDirectory() method. Let's see how it is done:

Create a temporary directory with PHP

Download the latest TurboCommons and TurboDepot phar files, place them on your project as dependencies and run the following code:

require 'path/to/your/dependencies/folder/turbocommons-php-X.X.X.phar';
require 'path/to/your/dependencies/folder/turbodepot-php-X.X.X.phar';

use org\turbodepot\src\main\php\managers\FilesManager;

$filesManager = new FilesManager();
$filesManager->createTempDirectory('temp-dir-name');

createTempDirectory() will generate a folder called 'temp-dir-name' at the specific OS temporary location. It will be automatically deleted after the PHP script ends (if possible). In case the 'temp-dir-name' is not available cause it already exists, an alternative non-existent name will be automatically generated. The full filesystem path to the new temp directory will be returned by this method.

Create a temporary directory with Javascript (node js)

Install the turbodepot dependency by executing the following command at the root of your project:

npm install turbodepot-node

And then run the following code:

const { FilesManager } = require('turbodepot-node');
let filesManager = new FilesManager();
filesManager.createTempDirectory('temp-dir-name');

createTempDirectory() will generate a folder called 'temp-dir-name' at the specific OS temporary location. It will be automatically deleted after the application finishes. In case the 'temp-dir-name' is not available cause it already exists, an alternative non-existent name will be automatically generated. The full filesystem path to the new temp directory will be returned by this method.

Create a temporary directory with Typescript (TS)

Install the turbodepot dependency by executing the following command at the root of your project:

npm install turbodepot-node

And then run the following code:

import { FilesManager } from 'turbodepot-node';
let filesManager = new FilesManager();
filesManager.createTempDirectory('temp-dir-name');

createTempDirectory() will generate a folder called 'temp-dir-name' at the specific OS temporary location. It will be automatically deleted after the application finishes. In case the 'temp-dir-name' is not available cause it already exists, an alternative non-existent name will be automatically generated. The full filesystem path to the new temp directory will be returned by this method.