{"id":15277487,"url":"https://github.com/rachyharkov/codeigniter4-media","last_synced_at":"2025-09-14T05:09:23.119Z","repository":{"id":185471894,"uuid":"673600069","full_name":"rachyharkov/codeigniter4-media","owner":"rachyharkov","description":"Codeigniter 4 Library for handling upload","archived":false,"fork":false,"pushed_at":"2024-09-04T12:30:17.000Z","size":106,"stargazers_count":8,"open_issues_count":3,"forks_count":1,"subscribers_count":1,"default_branch":"main","last_synced_at":"2025-08-19T02:26:49.153Z","etag":null,"topics":["codeigniter4-library","hacktoberfest","library","media-management"],"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/rachyharkov.png","metadata":{"files":{"readme":"README.md","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","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}},"created_at":"2023-08-02T02:22:15.000Z","updated_at":"2025-07-05T16:31:12.000Z","dependencies_parsed_at":null,"dependency_job_id":"d04225e4-d82d-4714-8635-7ab8c262438a","html_url":"https://github.com/rachyharkov/codeigniter4-media","commit_stats":null,"previous_names":["rachyharkov/codeigniter-4-media"],"tags_count":3,"template":false,"template_full_name":null,"purl":"pkg:github/rachyharkov/codeigniter4-media","repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rachyharkov%2Fcodeigniter4-media","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rachyharkov%2Fcodeigniter4-media/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rachyharkov%2Fcodeigniter4-media/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rachyharkov%2Fcodeigniter4-media/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/rachyharkov","download_url":"https://codeload.github.com/rachyharkov/codeigniter4-media/tar.gz/refs/heads/main","sbom_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/rachyharkov%2Fcodeigniter4-media/sbom","scorecard":null,"host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":275062966,"owners_count":25398888,"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","status":"online","status_checked_at":"2025-09-14T02:00:10.474Z","response_time":75,"last_error":null,"robots_txt_status":"success","robots_txt_updated_at":"2025-07-24T06:49:26.215Z","robots_txt_url":"https://github.com/robots.txt","online":true,"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":["codeigniter4-library","hacktoberfest","library","media-management"],"created_at":"2024-09-30T11:07:04.948Z","updated_at":"2025-09-14T05:09:23.095Z","avatar_url":"https://github.com/rachyharkov.png","language":"PHP","readme":"\u003c!-- PROJECT LOGO --\u003e\n\u003cbr /\u003e\n\u003cp align=\"center\"\u003e\n  \u003ca href=\"https://github.com/rachyharkov/codeigniter4-media\"\u003e\n    \u003cimg src=\"./src/assets/codeigniter-4-media-banner.png\" alt=\"Banner\"  height=\"200\"\u003e\n  \u003c/a\u003e\n  \n  \u003ch3 align=\"center\"\u003eCodeIgniter 4 Media Library\u003c/h3\u003e\n\u003c/p\u003e\n\nCodeigniter package for to handle media upload file task (at least help a bit for my current job). My main goal on this package is codeigniter 4 have a library that be able to handle task such as organize file upload with minimial line of code, also i'm inspired by Laravel Media Library, so i decided to make this package.\n\n[![Latest Version on Packagist][ico-version]][link-packagist]\n[![Total Downloads][ico-downloads]][link-downloads]\n[![Contributors][contributors-shield]][contributors-url]\n[![Forks][forks-shield]][forks-url]\n[![Stargazers][stars-shield]][stars-url]\n[![Issues][issues-shield]][issues-url]\n[![MIT License][license-shield]][license-url]\n[![LinkedIn][linkedin-shield]][linkedin-url]\n\n\u003c!-- TABLE OF CONTENTS --\u003e\n## Table of Contents\n\n- [Table of Contents](#table-of-contents)\n- [Getting Started](#getting-started)\n  - [Prerequisites](#prerequisites)\n  - [Installation](#installation)\n  - [Publishing Resource](#publishing-resource)\n  - [Setup Model](#setup-model)\n- [How to use?](#how-to-use)\n  - [Store single File](#store-single-file)\n  - [Store single file - store thumbnail](#store-single-file---store-thumbnail)\n  - [Store single File - with custom name](#store-single-file---with-custom-name)\n  - [Store Multi File - Different Name](#store-multi-file---different-name)\n  - [Store Multi File - Same Name](#store-multi-file---same-name)\n  - [Get Single File - Metadata](#get-single-file---metadata)\n  - [Get Single File - Just URL](#get-single-file---just-url)\n  - [Get All file of collection](#get-all-file-of-collection)\n  - [Query result with more data? Just assign it](#query-result-with-more-data-just-assign-it)\n  - [Delete file collection](#delete-file-collection)\n  - [Delete and upload? Of course you can do that](#delete-and-upload-of-course-you-can-do-that)\n  - [API Mode](#api-mode)\n- [Roadmap](#roadmap)\n- [Contributing](#contributing)\n- [License](#license)\n- [Contact](#contact)\n- [Support on Trakteer](#support-on-trakteer)\n\n\n\u003c!-- GETTING STARTED --\u003e\n## Getting Started\n\nThis is an example of how you may give instructions on setting up your project locally.\nTo get a local copy up and running follow these simple example steps.\n\n### Prerequisites\n\n- PHP 7.2+\n- CodeIgniter Framework (4.* recommended)\n- Composer\n- PHP sockets extension enabled\n\n### Installation\n\n```sh\ncomposer require rachyharkov/codeigniter4-media @dev\n```\n\n### Publishing Resource\nYou need to publish the resources for the default configuration\n```sh\nphp spark media:publish\n```\n\n### Migrate\nthen migrate new published table\n```sh\nphp spark migrate\n```\n\n### Setup Model\njust set your model like this\n\n```php\nuse CodeIgniter\\Model;\nuse Rachyharkov\\CodeigniterMedia\\HasMedia;\nuse Rachyharkov\\CodeigniterMedia\\InteractsWithMedia;\n\nclass User extends Model implements HasMedia\n{\n    use InteractsWithMedia;\n\n    // rest of codes\n}\n```\n\ndone\n\n## How to use?\n\n### Store single File\n\nusing name attribute of html input to let codeigniter4-media get the file, and add to specified collection (if no name entered, it will be using \"default\")\n\n```php\n$this-\u003euser_model-\u003einsert($data);\n$this-\u003euser_model-\u003eaddMediaFromRequest('photo')-\u003etoMediaCollection('profile_photo');\n```\n\n### Store single file - store thumbnail\n\nIt's useful if you want to store the thumbnail of the file, just add withThumbnail method after addMediaFromRequest method\n\n```php\n$this-\u003euser_model-\u003einsert($data);\n$this-\u003euser_model-\u003eaddMediaFromRequest('photo')-\u003ewithThumbnail()-\u003etoMediaCollection('profile_photo');\n```\n\n### Store single File - with custom name\n\nonly use usingFileName method after addMediaFromRequest method, this will be useful if you want to rename the file before store it to database\n\n```php\n$this-\u003euser_model-\u003einsert($data);\n$this-\u003euser_model-\u003eaddMediaFromRequest('photo')-\u003eusingFileName('data_'.random(20))-\u003etoMediaCollection('profile_photo');\n```\n\n### Store Multi File - Different Name\n\nstore file from multi different request name (for example, you have 2 input file with different input file name attribute value, and you want to store it to same collection)\n\n```php\n$this-\u003euser_model-\u003einsert($data);\n$this-\u003euser_model-\u003eaddMediaWithRequestCollectionMapping([\n      'file_input_photo' =\u003e 'profile_photo_collection',\n      'file_input_profile_cover' =\u003e 'profile_photo_collection'\n    ])\n```\n\n### Store Multi File - Same Name\n\nThis time using addMedia, make sure it's an file object (binary payload) to make addMedia accept value, in this example using multiple file input with same name attribute value\n\n```php\n$this-\u003eproduct_model-\u003einsert($data);\n$product_images = $this-\u003erequest-\u003egetFiles();\n\n    foreach ($product_images['photos'] as $key =\u003e $p) {\n    $this-\u003eproduct_model-\u003eaddMedia($p)-\u003etoMediaCollection('product_image');\n    }\n```\n\n### Get Single File - Metadata\n\n```php\n\n$user = $this-\u003euser_model-\u003ewhere('id', $id)-\u003efirst();\n$user-\u003ephoto = $this-\u003euser_model-\u003emediaOf('profile_photo')-\u003egetFirstMedia();\n\nreturn view('user/edit', $data);\n\n```\n\nabove will return null if no file meta information returned, handle it like this\n\n```php\n    \u003cimg src=\"\u003c?= $user-\u003emedia ? $user-\u003emedia-\u003efile_path.'/'.$user-\u003emedia-\u003efile_name : $user-\u003emedia ?\u003e\" alt=\"User Photo Profile\"\u003e\n```\n\n### Get Single File - Just URL\n\nThis is the example of how to assign new object to existing object (for example user object) with new property (photo) that contains the url of file\n\n```php\n$user = $this-\u003euser_model-\u003ewhere('id', $id)-\u003efirst();\n$user-\u003ephoto = $this-\u003euser_model-\u003emediaOf('profile_photo')-\u003egetFirstMediaUrl();\n\nreturn view('user/edit', $data);\n\n```\n\nIt's possible to pass parameter with 'thumb' value to get the thumbnail of the file\n\n```php\n$media-\u003egetFirstMediaUrl('thumb');\n```\n\n### Get All file of collection\n\n\n\n```php\n$user = $this-\u003euser_model-\u003ewhere('id', $id)-\u003efirst();\n$user-\u003ecollection_of_photo_profile = $this-\u003euser_model-\u003emediaOf('profile_photo');\n\n    return view('user/edit', $data);\n```\n\n### Query result with more data? Just assign it\nThe second parameter `mediaOf()` accept id of media who owned by the record\n```php\n$users  = $this-\u003eusers_model-\u003efindAll();\nforeach($users as $key =\u003e $value) {\n    $users[$key]-\u003ephoto = $this\n                            -\u003eticket_log_model\n                            -\u003emediaOf('profile_photo', $value-\u003eid)\n                            -\u003egetFirstMediaUrl();\n}\n```\n\n### Delete file collection\n\n```php\n$this-\u003eproduct_model-\u003ewhere('id', $product-\u003eid)-\u003edelete();\n$this-\u003euser_model-\u003eclearMediaCollection('profile_photo');\n```\n\n### Delete and upload? Of course you can do that\nJust like this after you find or update the record\n\n```php\n$this-\u003euser_model-\u003efind($id);\n\n    //or\n\n$this-\u003euser_model-\u003eupdate($data, $id);\n\n$this-\u003euser_model-\u003eclearMediaCollection('profile_photo');\n$this-\u003euser_model-\u003eaddMediaFromRequest('photo')-\u003etoMediaCollection('profile_photo');\n```\n\n### API Mode\n\ncreate method in your controller just like this, set asTemp to true if you want to return the file metadata (this is useful if you want to show the file right after upload process completed, make sure to return it)\n\n🪄 **Backend - Store File**\n\n```php\npublic function api_upload()\n{\n$user_id = $this-\u003erequest-\u003egetVar('user_id');\n    return $this-\u003euser_model-\u003eaddMediaFromRequest('file')-\u003etoMediaCollection('profile_photo')-\u003eresponseJson();\n}\n```\n\nyou will get this response\n\n```php\n{\n    status: \"success\",\n    message: \"File uploaded successfuly\",\n    data: {\n        collection_name: \"profile_photo\"\n        file_ext: \"jpg\"\n        file_name: \"default\"\n        file_path: \"uploads/profile_photo\"\n        file_size: 62431\n        file_type: \"image/jpeg\"\n        model_id: \"200090\"\n        model_type: \"App\\\\Models\\\\User\"\n        orig_name: \"20211128_165410.jpg\"\n        unique_name:  \"1691502324_94b5e01970c97f5ac670.jpg\"\n    }\n}\n```\n\n🪄 **Backend - Delete File**\n\nOn your controller, create method like this (make sure to return the responseJson method)\n\n```php\n    public function api_delete()\n    {\n        return $this-\u003euser_model-\u003eclearMediaCollection(request()-\u003egetVar('temp_id'), 'profile_photo')-\u003eresponseJson();\n    }\n```\n\nYou will get this response\n\n```php\n    {\n        status: \"success\",\n        message: \"File 1691502324_94b5e01970c97f5ac670.jpg deleted successfuly\",\n    }\n```\n\n\u003c!-- ROADMAP --\u003e\n## Roadmap\n\nSee the [open issues](https://github.com/rachyharkov/codeigniter4-media/issues) for a list of proposed features (and known issues).\n\n\n\u003c!-- CONTRIBUTING --\u003e\n## Contributing\n\nContributions are what makes the open source community such an amazing place to learn, inspire, and create. Any contributions you make are **greatly appreciated**.\n\n1. Fork the Project\n2. Create your Feature Branch (`git checkout -b feature/AmazingFeature`)\n3. Commit your Changes (`git commit -m 'Add some AmazingFeature'`)\n4. Push to the Branch (`git push origin feature/AmazingFeature`)\n5. Open a Pull Request\n\n\n\u003c!-- LICENSE --\u003e\n## License\n\nDistributed under the MIT License. See `LICENSE` for more information.\n\n\u003c!-- CONTACT --\u003e\n## Contact\n\nRachmad Nur Hayat - [https://rachmad.dev](https://rachmad.dev) - rachmadnurhayat@gmail.com\n\n\n## Support on Trakteer\nlove what i made?, please support me on [Trakteer](https://trakteer.id/rachyharkov)\n\nfrom overseas? you can [Paypal](https://paypal.me/rachyharkov?country.x=ID\u0026locale.x=en_US) me\n\n\u003cp align=\"center\"\u003e\n    \u003ca href=\"https://teer.id/rachmadnh\" target=\"_blank\"\u003e \u003cimg align=\"left\" src=\"https://trakteer-assets.sgp1.digitaloceanspaces.com/images/mix/navbar-logo.png?date=18-11-2023\" height=\"30\" alt=\"rachyharkov\" /\u003e\u003c/a\u003e\n    \u003c/a\u003e\n    \u003ca href=\"https://teer.id/rachmadnh\" target=\"_blank\"\u003e \u003cimg align=\"left\" src=\"https://static-00.iconduck.com/assets.00/paypal-icon-2048x547-tu0aql1a.png\" height=\"30\" alt=\"rachyharkov\" /\u003e\u003c/a\u003e\n    \u003c/a\u003e\n\u003c/p\u003e\n\n\n\u003c!-- MARKDOWN LINKS \u0026 IMAGES --\u003e\n\u003c!-- https://www.markdownguide.org/basic-syntax/#reference-style-links --\u003e\n[contributors-shield]: https://img.shields.io/github/contributors/rachyharkov/codeigniter4-media.svg?style=flat-square\n[contributors-url]: https://github.com/rachyharkov/codeigniter4-media/graphs/contributors\n[forks-shield]: https://img.shields.io/github/forks/rachyharkov/codeigniter4-media.svg?style=flat-square\n[forks-url]: https://github.com/rachyharkov/codeigniter4-media/network/members\n[stars-shield]: https://img.shields.io/github/stars/rachyharkov/codeigniter4-media.svg?style=flat-square\n[stars-url]: https://github.com/rachyharkov/codeigniter4-media/stargazers\n[issues-shield]: https://img.shields.io/github/issues/rachyharkov/codeigniter4-media.svg?style=flat-square\n[issues-url]: https://github.com/rachyharkov/codeigniter4-media/issues\n[license-shield]: https://img.shields.io/github/license/rachyharkov/codeigniter4-media.svg?style=flat-square\n[license-url]: https://github.com/rachyharkov/codeigniter4-media/blob/main/LICENSE.txt\n[linkedin-shield]: https://img.shields.io/badge/-LinkedIn-black.svg?style=flat-square\u0026logo=linkedin\u0026colorB=555\n[linkedin-url]: https://www.linkedin.com/in/rachmad-nur-hayat-731a391b2/\n[product-screenshot]: images/screenshot.png\n\n[ico-version]: https://img.shields.io/packagist/v/rachyharkov/codeigniter4-media.svg?style=flat-square\n[ico-downloads]: https://img.shields.io/packagist/dt/rachyharkov/codeigniter4-media.svg?style=flat-square\n\n[link-packagist]: https://packagist.org/packages/rachyharkov/codeigniter4-media\n[link-downloads]: https://packagist.org/packages/rachyharkov/codeigniter4-media\n[link-author]: https://github.com/rachyharkov\n","funding_links":["https://paypal.me/rachyharkov?country.x=ID\u0026locale.x=en_US"],"categories":[],"sub_categories":[],"project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frachyharkov%2Fcodeigniter4-media","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Frachyharkov%2Fcodeigniter4-media","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Frachyharkov%2Fcodeigniter4-media/lists"}