{"id":19951977,"url":"https://github.com/studio24/rotate","last_synced_at":"2026-03-07T18:32:44.505Z","repository":{"id":57060542,"uuid":"54915862","full_name":"studio24/rotate","owner":"studio24","description":"Simple file rotation utility ","archived":false,"fork":false,"pushed_at":"2019-02-02T13:06:21.000Z","size":71,"stargazers_count":17,"open_issues_count":1,"forks_count":3,"subscribers_count":1,"default_branch":"master","last_synced_at":"2026-03-05T05:06:00.424Z","etag":null,"topics":["open-source"],"latest_commit_sha":null,"homepage":"","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/studio24.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.md","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null}},"created_at":"2016-03-28T18:54:20.000Z","updated_at":"2025-08-27T12:01:50.000Z","dependencies_parsed_at":"2022-08-24T14:53:32.684Z","dependency_job_id":null,"html_url":"https://github.com/studio24/rotate","commit_stats":null,"previous_names":[],"tags_count":2,"template":false,"template_full_name":null,"purl":"pkg:github/studio24/rotate","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/studio24%2Frotate","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/studio24%2Frotate/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/studio24%2Frotate/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/studio24%2Frotate/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/studio24","download_url":"https://codeload.github.com/studio24/rotate/tar.gz/refs/heads/master","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/studio24%2Frotate/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":286080680,"owners_count":30226247,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2026-03-07T18:12:09.766Z","status":"ssl_error","status_checked_at":"2026-03-07T18:11:58.786Z","response_time":53,"last_error":"SSL_read: unexpected eof while reading","robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":false,"can_crawl_api":true,"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":["open-source"],"created_at":"2024-11-13T01:11:05.453Z","updated_at":"2026-03-07T18:32:43.297Z","avatar_url":"https://github.com/studio24.png","language":"PHP","funding_links":[],"categories":[],"sub_categories":[],"readme":"# Rotate\n\nSimple file rotation utility which rotates and removes old files or folders, useful where you cannot use logrotate (e.g. a Windows system) \nor you want to rotate or delete files based on a timestamp or date contained in the filename. \n\n## Installation\n\n```sh\ncomposer require studio24/rotate\n```\n\n## Usage\n\nYou can use Rotate in two modes: rotate (renames files and removes oldest files) or delete (deletes files according to a pattern).\n\nImport at the top of your PHP script via:\n\n```sh\nuse studio24\\Rotate\\Rotate;\n```\n\n### Setting the filename format to match\n\nBoth Rotate and Delete pass in the filename pattern you want to match for in the constructor or via the `setFilenameFormat()` method.\nThis can be used to match a single file, files matching a pattern, or files with a datetime within the filename pattern. \n\nRotate only works on files. Delete can also delete folders and recursively deletes any child files in that folder. \n\n#### Matches on leaf elements\n\nPlease note files are matched on the last leaf element. All files in the parent folder are scanned, files (and folders \nfor Delete) are checked to see whether they match the specified pattern.\n\nFor example passing `path/to/*.log` will search for all files ending .log in the folder `path/to`.\n\n#### Filename patterns\n\nThe following patterns are supported when matching files or folders:\n\n* `debug.log` - exact match for a file called debug.log\n* `*` - matches any string, for example `*.log` matches all files ending .log\n* `{Ymd}` - matches time segment in a file, for example `order.{Ymd}.log` matches a file in the format order.20160401.log\n\n#### Datetime formats\n\nFor datetime formats, any date format supported by [DateTime::createFromFormat](http://php.net/datetime.createfromformat) is allowed \nexcluding the Timezone identifier `e` and whitespace and separator characters. \n\n### Deleting folders recursively\n\nYou can also delete folders and all child files that match. This is, however, dangerous so by default no folders can be deleted. You \n need to explicitly add folder paths that are safe to delete via `Delete::addSafeRecursiveDeletePath($path)`. When using \n this function you need to use the full absolute path. Use `realpath()` to expand full paths if you need to.\n\nFor example, if you want to delete all folders in `/var/www/test/staging/data/logs/` that are 1+ months old:\n\n```\n$rotate = new Delete('/var/www/test/staging/data/logs/old-logs/*');\n$rotate-\u003eaddSafeRecursiveDeletePath('/var/www/test/staging/data/logs/');\n$files = $rotate-\u003edeleteByFilenameTime('1 month');\n```\n\nThe above code would allow you to delete folders within `/var/www/test/staging/data/logs/` only. \n\n### Rotate\n\nRotate log files in a similar manner to logrotate.\n\nThe following example rotates the file debug.log, this renames debug.log to debug.log.1, debug.log.1 to debug.log.2, debug.log.2 to \ndebug.log.3 and so on. It keeps 10 copies, so it deletes debug.log.10 and renames debug.log.9 to debug.log.10.\n\n```sh\nuse studio24\\Rotate\\Rotate;\n\n$rotate = new Rotate('path/to/debug.log');\n$rotate-\u003erun();\n```\n\n#### How many copies to keep\n\nRotate keeps 10 copies of files by default, you can change this via:\n\n```\n$rotate-\u003ekeep(20);\n```\n\n#### Rotated based on filesize\nYou can only rotate files when they reach a certain filesize, rather than automatically rotate each time the `$rotate-\u003erun()` method is run.\n \n```\n$rotate-\u003esize(\"12MB\");\n```\n\n### Delete\n\nDeletes files based on modification time, datetime in the filename, or based on a custom callback function.\n\n#### Time-based\n\nDeletes files based on the modification time. For example, to delete all JPG files in a folder over 3 months old: \n\n```sh\nuse studio24\\Rotate\\Delete;\n\n$rotate = new Delete('path/to/images/*.jpg');\n$deletedFiles = $rotate-\u003edeleteByFileModifiedDate('3 months');\n```\n\nThe `deleteByFileModifiedDate()` method accepts either a valid DateInterval object or a relative date format as specified on \n[Relative Formats](http://php.net/manual/en/datetime.formats.relative.php).\n\n#### Time format in filename\nDeletes files based on the datetime in the filename. For example, to delete all  order logfiles with a date in their \nfilename over 3 months old:\n\n```sh\nuse studio24\\Rotate\\Delete;\n\n$rotate = new Delete('path/to/logs/orders.YYYYMMDD.log');\n$deletedFiles = $rotate-\u003edeleteByFilenameTime('3 months');\n```\n\n#### Based on a custom callback \nDeletes files based on a custom callback function, this is useful if you need to perform more complex code to assess whether a file\nshould be deleted. For example, to delete all image files called 1000.jpg and below:\n\n```sh\nuse studio24\\Rotate\\Delete;\nuse studio24\\Rotate\\DirectoryIterator;\n\n$rotate = new Delete('path/to/logs/*.jpg');\n$deletedFiles = $rotate-\u003edeleteByCallback(function(DirectoryIterator $file){\n    if ($file-\u003egetBasename() \u003c= 1000) {\n        return true;\n    } \n    return false;\n});\n```\n\n##### DirectoryIterator\nThe callback function must accept one parameter `$file`, which is of type `\\studio24\\Rotate\\DirectoryIterator`.\n\nThis iterator has the following methods in addition to the normal [SPL DirectoryIterator](http://php.net/DirectoryIterator).\n\n* `isMatch()` - whether the current file matches the filename patter we are looking for\n* `hasDate()` - whether the current file has a datetime within the filename\n* `getFilenameDate()` - return the datetime from the current file (as a DateTime object)\n\n## License\n\nThe MIT License (MIT). Please see [License File](LICENSE.md) for more information.\n\n## Credits\n\n- [Simon R Jones](https://github.com/simonrjones)","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstudio24%2Frotate","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Fstudio24%2Frotate","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Fstudio24%2Frotate/lists"}