Title: Working with the /tmp directory
Author: WordPress VIP Documentation
Published: September 10, 2020
Last modified: January 1, 2026

---

 1. [VIP File System](https://docs.wpvip.com/vip-file-system/)
 2. Working with the /tmp directory

#  Working with the /tmp directory

The system temp directory (`/tmp`) is a local, writeable path on an application’s
web server on the VIP Platform. The `/tmp` directory is most commonly used to perform
local manipulations to temporary files. For example, transferring a ZIP archive 
to `/tmp`, extracting its contents, then uploading the contents to the WordPress
site’s `wp-content/uploads/` directory on the VIP File System.

[All filesystem functions](https://www.php.net/manual/en/ref.filesystem.php) will
work as expected in `/tmp`, including directory traversal.

## Considerations

 * Use [`get_temp_dir()`](https://developer.wordpress.org/reference/functions/get_temp_dir/)
   to determine the correct, writable directory path for temporary files.
 * If a temporary file needs to be generated, use `[wp_tempnam()](https://developer.wordpress.org/reference/functions/wp_tempnam/)`.
 * Use [`unlink()`](http://php.net/manual/en/function.unlink.php) to clean up and
   remove all local temporary files after they are no longer needed. Be sure to 
   remove the files before the process that created them terminates.
 * Files and directories can only be relied on for the duration of the current request.
    - Each request may be served by a different container and the sequence of requests
      from a given user are not guaranteed to be served by the same container.
    - Containers are transient and may be created and destroyed due to autoscaling.

## Code example for extracting the contents of a zip file

ZIP files can be uploaded to `wp-content/uploads/`, but the file’s contents cannot
be extracted from within that directory. To extract the contents of a ZIP file, 
it must be temporarily copied to the system temp directory (`/tmp`) of an application’s
web servers.

The code example below demonstrates this process for a ZIP file that was uploaded
to the WordPress Media Library with attachment ID `1234`. The ZIP file is programmatically
copied to `/tmp` with the file name `updated.csv.zip`. The file is then decompressed,
and the contents are programmatically uploaded to `wp-content/uploads/`.

Because [the VIP File System lacks a true directory structure](https://docs.wpvip.com/vip-file-system/media-uploads/),
operations like `scandir()` will not perform as expected. However, in `/tmp` a true
directory structure _does_ exist, and `scandir()` and other directory-related operations
can be used. In a scenario where the name of the extracted file is unknown, the 
code example below could be modified to use `scandir()` to read the names of files
that are extracted to `/tmp`.

    ```lang-php
    // ZIP files can only be extracted in the system /tmp directory.
    $tmp_path = get_temp_dir();
    $upload_dir = wp_get_upload_dir()['basedir'];

    // The attachment ID of a ZIP file that was uploaded to the Media Library.
    $zip_attachment = get_attached_file( 1234 );

    // The path and file name on /tmp where the file will be copied.
    $zip_tmp_file = $tmp_path . '/updated.csv.zip';

    // Copy the ZIP file to /tmp for extraction.
    copy( $zip_attachment, $zip_tmp_file );

    if ( file_exists( $zip_tmp_file ) ) {

        $zip = new ZipArchive;
        $result = $zip->open( $zip_tmp_file ); 

        if ( $result === true ) {

            // Extract the contents of the ZIP file to the system /tmp directory.
            $zip->extractTo( $tmp_path );
            $zip->close();

            // For this example, the filename extracted from the ZIP file is known.
            $extracted_filename = $tmp_path . '/csv/updated.csv';
            $file_path = $upload_dir . '/csv/updated.csv';

            // Copy the extracted file to wp-content/uploads/
            // Note: copy() will overwrite existing files! 
            // Verify that a file does not already exist with the same name.
            if( ! file_exists( $file_path ) ) {
    			copy( $extracted_filename, $file_path );
    			// The extracted file is now accessible at:
    			// https://example-com.go-vip.net/wp-content/uploads/csv/updated.csv

    			// Clean up the extracted file from /tmp
    			unlink(  $extracted_filename );
    		}

    		// Clean up the ZIP file from /tmp
    		unlink( $zip_tmp_file )

        }
    }
    ```

Last updated: January 01, 2026