{"id":13548949,"url":"https://github.com/spatie/temporary-directory","last_synced_at":"2025-05-13T16:11:07.188Z","repository":{"id":41559640,"uuid":"80403728","full_name":"spatie/temporary-directory","owner":"spatie","description":"A simple class to work with a temporary directory","archived":false,"fork":false,"pushed_at":"2025-01-27T12:58:32.000Z","size":141,"stargazers_count":944,"open_issues_count":0,"forks_count":46,"subscribers_count":5,"default_branch":"main","last_synced_at":"2025-04-14T02:57:02.434Z","etag":null,"topics":["directory","php","storage","testing"],"latest_commit_sha":null,"homepage":"https://spatie.be/opensource/php","language":"PHP","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/spatie.png","metadata":{"files":{"readme":"README.md","changelog":"CHANGELOG.md","contributing":null,"funding":".github/FUNDING.yml","license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null},"funding":{"github":"spatie","custom":"https://spatie.be/open-source/support-us"}},"created_at":"2017-01-30T08:14:51.000Z","updated_at":"2025-04-04T04:41:10.000Z","dependencies_parsed_at":"2023-02-16T07:10:19.643Z","dependency_job_id":"54572fd3-56df-4483-8e63-0aafd24d83af","html_url":"https://github.com/spatie/temporary-directory","commit_stats":{"total_commits":145,"total_committers":32,"mean_commits":4.53125,"dds":0.7379310344827585,"last_synced_commit":"282e88afef8c6a72910f78e9809b0e02851c9ee7"},"previous_names":[],"tags_count":23,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Ftemporary-directory","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Ftemporary-directory/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Ftemporary-directory/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/spatie%2Ftemporary-directory/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/spatie","download_url":"https://codeload.github.com/spatie/temporary-directory/tar.gz/refs/heads/main","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250166345,"owners_count":21385761,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["directory","php","storage","testing"],"created_at":"2024-08-01T12:01:16.394Z","updated_at":"2025-04-23T20:57:51.033Z","avatar_url":"https://github.com/spatie.png","language":"PHP","funding_links":["https://github.com/sponsors/spatie","https://spatie.be/open-source/support-us"],"categories":["PHP"],"sub_categories":[],"readme":"# Quickly create, use and delete temporary directories\n\n[![Latest Version on Packagist](https://img.shields.io/packagist/v/spatie/temporary-directory.svg?style=flat-square)](https://packagist.org/packages/spatie/temporary-directory)\n![Tests](https://github.com/spatie/temporary-directory/workflows/run-tests/badge.svg?label=tests)\n[![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE.md)\n[![Total Downloads](https://img.shields.io/packagist/dt/spatie/temporary-directory.svg?style=flat-square)](https://packagist.org/packages/spatie/temporary-directory)\n\nThis package allows you to quickly create, use and delete a temporary directory in the system's temporary directory.\n\nHere's a quick example on how to create a temporary directory and delete it:\n\n```php\nuse Spatie\\TemporaryDirectory\\TemporaryDirectory;\n\n$temporaryDirectory = (new TemporaryDirectory())-\u003ecreate();\n\n// Get a path inside the temporary directory\n$temporaryDirectory-\u003epath('temporaryfile.txt');\n\n// Delete the temporary directory and all the files inside it\n$temporaryDirectory-\u003edelete();\n```\n\n## Support us\n\n[\u003cimg src=\"https://github-ads.s3.eu-central-1.amazonaws.com/temporary-directory.jpg?t=1\" width=\"419px\" /\u003e](https://spatie.be/github-ad-click/temporary-directory)\n\nWe invest a lot of resources into creating [best in class open source packages](https://spatie.be/open-source). You can support us by [buying one of our paid products](https://spatie.be/open-source/support-us).\n\nWe highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on [our contact page](https://spatie.be/about-us). We publish all received postcards on [our virtual postcard wall](https://spatie.be/open-source/postcards).\n\n## Installation\n\nYou can install the package via composer:\n\n```bash\ncomposer require spatie/temporary-directory\n```\n\n## Usage\n\n### Creating a temporary directory\n\nTo create a temporary directory simply call the `create` method on a `TemporaryDirectory` object.\n\n```php\n(new TemporaryDirectory())-\u003ecreate();\n```\n\nAlternatively, use the static `make` method on a `TemporaryDirectory` object.\n\n```php\nTemporaryDirectory::make();\n```\n\nBy default, the temporary directory will be created in a timestamped directory in your system's temporary directory (usually `/tmp`).\n\n### Naming your temporary directory\n\nIf you want to use a custom name for your temporary directory instead of the timestamp call the `name` method with a string `$name` argument before the `create` method.\n\n```php\n(new TemporaryDirectory())\n   -\u003ename($name)\n   -\u003ecreate();\n```\n\nBy default an exception will be thrown if a directory already exists with the given argument. You can override this behaviour by calling the `force` method in combination with the `name` method.\n\n```php\n(new TemporaryDirectory())\n   -\u003ename($name)\n   -\u003eforce()\n   -\u003ecreate();\n```\n\n### Setting a custom location for a temporary directory\n\nYou can set a custom location in which your temporary directory will be created by passing a string `$location` argument to the `TemporaryDirectory` constructor.\n\n```php\n(new TemporaryDirectory($location))\n   -\u003ecreate();\n```\n\nThe `make` method also accepts a `$location` argument.\n\n```php\nTemporaryDirectory::make($location);\n```\n\nFinally, you can call the `location` method with a `$location` argument.\n\n```php\n(new TemporaryDirectory())\n   -\u003elocation($location)\n   -\u003ecreate();\n```\n\n### Determining paths within the temporary directory\n\nYou can use the `path` method to determine the full path to a file or directory in the temporary directory:\n\n```php\n$temporaryDirectory = (new TemporaryDirectory())-\u003ecreate();\n$temporaryDirectory-\u003epath('dumps/datadump.dat'); // return  /tmp/1485941876276/dumps/datadump.dat\n```\n\n### Emptying a temporary directory\n\nUse the `empty` method to delete all the files inside the temporary directory.\n\n```php\n$temporaryDirectory-\u003eempty();\n```\n\n### Deleting a temporary directory\n\nOnce you're done processing your temporary data you can delete the entire temporary directory using the `delete` method. All files inside of it will be deleted.\n\n```php\n$temporaryDirectory-\u003edelete();\n```\n\n### Deleting a temporary directory when the object is destroyed\n\nIf you want to automatically have the filesystem directory deleted when the object instance has no more references in\nits defined scope, you can enable `deleteWhenDestroyed()` on the TemporaryDirectory object.\n\n```php\nfunction handleTemporaryFiles()\n{\n    $temporaryDirectory = (new TemporaryDirectory())\n        -\u003edeleteWhenDestroyed()\n        -\u003ecreate();\n\n    // ... use the temporary directory\n\n    return; // no need to manually call $temporaryDirectory-\u003edelete()!\n}\n\nhandleTemporaryFiles();\n```\n\nYou can also call `unset()` on an object instance.\n\n## Testing\n\n```bash\ncomposer test\n```\n\n## Changelog\n\nPlease see [CHANGELOG](CHANGELOG.md) for more information on what has changed recently.\n\n## Contributing\n\nPlease see [CONTRIBUTING](https://github.com/spatie/.github/blob/main/CONTRIBUTING.md) for details.\n\n## Security Vulnerabilities\n\nPlease review [our security policy](../../security/policy) on how to report security vulnerabilities.\n\n## Postcardware\n\nYou're free to use this package, but if it makes it to your production environment we highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using.\n\nOur address is: Spatie, Kruikstraat 22, 2018 Antwerp, Belgium.\n\nWe publish all received postcards [on our company website](https://spatie.be/en/opensource/postcards).\n\n## Credits\n\n- [Alex Vanderbist](https://github.com/AlexVanderbist)\n- [All Contributors](../../contributors)\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspatie%2Ftemporary-directory","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fspatie%2Ftemporary-directory","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fspatie%2Ftemporary-directory/lists"}