spatie/temporary-directory

repository·main·Indexed 21 days ago

https://github.com/spatie/temporary-directory

A PHP utility package to create, manage, and clean up temporary directories. It provides a fluent interface to configure directory names, custom locations, and permissions, with features for automatic deletion upon object destruction and absolute path resolution for internal files.

Tokens
2K
Snippets
13
Records
14
Agent score
74%

What's inside spatie/temporary-directory

  1. Automatically delete a temporary directory when destroyed

    main

    If you want the directory to be automatically deleted when the TemporaryDirectory object is no longer referenced (e.g., when it goes out of scope or is unset()), call deleteWhenDestroyed() before creating it. This removes the need to manually call delete().

    function handleTemporaryFiles()
    {
        $temporaryDirectory = (new TemporaryDirectory())
            ->deleteWhenDestroyed()
            ->create();
    
        // ... use the temporary directory
    
        return; // The directory is automatically deleted here
    }
    
    handleTemporaryFiles();
  2. Set custom permissions for a temporary directory

    main

    The default permissions are 0777. You can customize this by calling the permission($permission) method with an integer argument before calling create().

    (new TemporaryDirectory())
       ->permission(0755)
       ->create();
  3. Determine paths within the temporary directory

    main

    Use the path($path) method to get the full absolute path to a file or subdirectory inside your temporary directory.

    $temporaryDirectory = (new TemporaryDirectory())->create();
    $filePath = $temporaryDirectory->path('dumps/datadump.dat'); 
    // Returns something like: /tmp/1485941876276/dumps/datadump.dat
  4. Empty or delete a temporary directory

    main

    To remove all files inside the directory without deleting the directory itself, use empty(). To delete the entire directory and all its contents, use delete().

    // Delete all files inside the directory
    $temporaryDirectory->empty();
    
    // Delete the entire directory and its contents
    $temporaryDirectory->delete();
  5. Name your temporary directory

    main

    To use a specific name instead of a timestamp, call the name($name) method before create(). If a directory with that name already exists, an exception will be thrown unless you also call force() to override this behavior.

    // Using a custom name
    (new TemporaryDirectory())
       ->name($name)
       ->create();
    
    // Using a custom name and forcing creation if it already exists
    (new TemporaryDirectory())
       ->name($name)
       ->force()
       ->create();
  6. Set a custom location for a temporary directory

    main

    You can specify where the temporary directory should be created using one of three ways: passing a $location string to the constructor, passing it to the make() method, or calling the location($location) method.

    // Via constructor
    (new TemporaryDirectory($location))->create();
    
    // Via static make() method
    TemporaryDirectory::make($location);
    
    // Via location() method
    (new TemporaryDirectory())->location($location)->create();
  7. Create a temporary directory

    main

    You can create a temporary directory using the create() method on a TemporaryDirectory instance, or by using the static make() method. By default, the directory is created in your system's temporary directory (e.g., /tmp) with a timestamped name.

    use Spatie\TemporaryDirectory\TemporaryDirectory;
    
    // Option 1: Using create()
    $temporaryDirectory = (new TemporaryDirectory())->create();
    
    // Option 2: Using the static make() method
    $temporaryDirectory = TemporaryDirectory::make();
  8. Create a temporary directory with `TemporaryDirectory::make()`

    main

    The quickest way to create a temporary directory is using the static make() method. This method instantiates the class and immediately calls create(). If no location is provided, it defaults to the system's temporary directory and generates a unique random name.

    use Spatie\TemporaryDirectory\TemporaryDirectory;
    
    $temporaryDirectory = TemporaryDirectory::make();
    
    $path = $temporaryDirectory->path();
  9. Configure a temporary directory before creation

    main

    You can use a fluent interface to configure the directory's name, location, permissions, and creation behavior before calling create().

    • name(string $name): Sets a specific name for the directory. Throws InvalidDirectoryName if the name contains illegal characters (\/?%*:|"<>).
    • location(string $location): Sets the parent directory where the temporary folder will reside.
    • permission(int $permission): Sets the octal permissions for the directory (default is 0777).
    • force(): If called, the directory will be deleted if it already exists before creating a new one.
    • deleteWhenDestroyed(bool $deleteWhenDestroyed): If set to true, the directory will be automatically deleted when the TemporaryDirectory object is destroyed (garbage collected or end of scope).
    $temporaryDirectory = TemporaryDirectory::make()
        ->location('/custom/path')
        ->name('my-temp-folder')
        ->permission(0755)
        ->force()
        ->deleteWhenDestroyed();
  10. Get paths for files within the temporary directory

    main

    Use the path() method to retrieve the absolute path of the temporary directory or to generate paths for files/subdirectories within it.

    • Calling path() without arguments returns the full path of the temporary directory.
    • Calling path('filename.txt') returns the path to that specific file inside the temporary directory. If the parent directory for that path does not exist, it will be created automatically.
    $directory = TemporaryDirectory::make();
    
    // Get the directory path
    $dirPath = $directory->path();
    
    // Get a path for a specific file inside
    $filePath = $directory->path('reports/daily_report.pdf');