PHPackages                             rachyharkov/codeigniter4-media - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [File &amp; Storage](/categories/file-storage)
4. /
5. rachyharkov/codeigniter4-media

ActiveLibrary[File &amp; Storage](/categories/file-storage)

rachyharkov/codeigniter4-media
==============================

Codeigniter 4 library for managing file or media and handling upload behavior

91443[2 PRs](https://github.com/rachyharkov/codeigniter4-media/pulls)PHP

Since Sep 4Pushed 1y ago1 watchersCompare

[ Source](https://github.com/rachyharkov/codeigniter4-media)[ Packagist](https://packagist.org/packages/rachyharkov/codeigniter4-media)[ RSS](/packages/rachyharkov-codeigniter4-media/feed)WikiDiscussions main Synced 1mo ago

READMEChangelog (3)DependenciesVersions (1)Used By (0)

 [ ![Banner](./src/assets/codeigniter-4-media-banner.png) ](https://github.com/rachyharkov/codeigniter4-media)

### CodeIgniter 4 Media Library

[](#codeigniter-4-media-library)

Codeigniter 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.

[![Latest Version on Packagist](https://camo.githubusercontent.com/a9d296edc137a39234e714e6e90747e5453b1853aad98d407f77f49f508b2d7a/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f762f72616368796861726b6f762f636f646569676e69746572342d6d656469612e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/rachyharkov/codeigniter4-media)[![Total Downloads](https://camo.githubusercontent.com/d452ea9cecb37d11eb9b1e1cf35a2433a8c4da7560388899e05bb2769faad6ad/68747470733a2f2f696d672e736869656c64732e696f2f7061636b61676973742f64742f72616368796861726b6f762f636f646569676e69746572342d6d656469612e7376673f7374796c653d666c61742d737175617265)](https://packagist.org/packages/rachyharkov/codeigniter4-media)[![Contributors](https://camo.githubusercontent.com/2831225e7f0128aebe10c85eb3810a16459021adc439562fb84ea9420f58e1d0/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f636f6e7472696275746f72732f72616368796861726b6f762f636f646569676e69746572342d6d656469612e7376673f7374796c653d666c61742d737175617265)](https://github.com/rachyharkov/codeigniter4-media/graphs/contributors)[![Forks](https://camo.githubusercontent.com/e9959a2a9e1d91060d815814e29a5184344c20d9b376ffabd8c05889370e0783/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f666f726b732f72616368796861726b6f762f636f646569676e69746572342d6d656469612e7376673f7374796c653d666c61742d737175617265)](https://github.com/rachyharkov/codeigniter4-media/network/members)[![Stargazers](https://camo.githubusercontent.com/281bd9c33c110ee71b4290e837e75d224806ead3c7f959e4816b610af06b66d0/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f73746172732f72616368796861726b6f762f636f646569676e69746572342d6d656469612e7376673f7374796c653d666c61742d737175617265)](https://github.com/rachyharkov/codeigniter4-media/stargazers)[![Issues](https://camo.githubusercontent.com/a36283cda64a8bb8e0834090901d70b45c4a8fe6f16aebd61b0224581de96ed7/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6973737565732f72616368796861726b6f762f636f646569676e69746572342d6d656469612e7376673f7374796c653d666c61742d737175617265)](https://github.com/rachyharkov/codeigniter4-media/issues)[![MIT License](https://camo.githubusercontent.com/c2dec300bf478a3cf9b0c94431cd2e4e370244101a379034d44c9be131c87683/68747470733a2f2f696d672e736869656c64732e696f2f6769746875622f6c6963656e73652f72616368796861726b6f762f636f646569676e69746572342d6d656469612e7376673f7374796c653d666c61742d737175617265)](https://github.com/rachyharkov/codeigniter4-media/blob/main/LICENSE.txt)[![LinkedIn](https://camo.githubusercontent.com/737807e0d19894612aa0ec4f81b4d4044034b9fbc425dfc88ca496e9eb98e535/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f2d4c696e6b6564496e2d626c61636b2e7376673f7374796c653d666c61742d737175617265266c6f676f3d6c696e6b6564696e26636f6c6f72423d353535)](https://www.linkedin.com/in/rachmad-nur-hayat-731a391b2/)

Table of Contents
-----------------

[](#table-of-contents)

- [Table of Contents](#table-of-contents)
- [Getting Started](#getting-started)
    - [Prerequisites](#prerequisites)
    - [Installation](#installation)
    - [Publishing Resource](#publishing-resource)
    - [Setup Model](#setup-model)
- [How to use?](#how-to-use)
    - [Store single File](#store-single-file)
    - [Store single file - store thumbnail](#store-single-file---store-thumbnail)
    - [Store single File - with custom name](#store-single-file---with-custom-name)
    - [Store Multi File - Different Name](#store-multi-file---different-name)
    - [Store Multi File - Same Name](#store-multi-file---same-name)
    - [Get Single File - Metadata](#get-single-file---metadata)
    - [Get Single File - Just URL](#get-single-file---just-url)
    - [Get All file of collection](#get-all-file-of-collection)
    - [Query result with more data? Just assign it](#query-result-with-more-data-just-assign-it)
    - [Delete file collection](#delete-file-collection)
    - [Delete and upload? Of course you can do that](#delete-and-upload-of-course-you-can-do-that)
    - [API Mode](#api-mode)
- [Roadmap](#roadmap)
- [Contributing](#contributing)
- [License](#license)
- [Contact](#contact)
- [Support on Trakteer](#support-on-trakteer)

Getting Started
---------------

[](#getting-started)

This is an example of how you may give instructions on setting up your project locally. To get a local copy up and running follow these simple example steps.

### Prerequisites

[](#prerequisites)

- PHP 7.2+
- CodeIgniter Framework (4.\* recommended)
- Composer
- PHP sockets extension enabled

### Installation

[](#installation)

```
composer require rachyharkov/codeigniter4-media @dev
```

### Publishing Resource

[](#publishing-resource)

You need to publish the resources for the default configuration

```
php spark media:publish
```

### Migrate

[](#migrate)

then migrate new published table

```
php spark migrate
```

### Setup Model

[](#setup-model)

just set your model like this

```
use CodeIgniter\Model;
use Rachyharkov\CodeigniterMedia\HasMedia;
use Rachyharkov\CodeigniterMedia\InteractsWithMedia;

class User extends Model implements HasMedia
{
    use InteractsWithMedia;

    // rest of codes
}
```

done

How to use?
-----------

[](#how-to-use)

### Store single File

[](#store-single-file)

using 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")

```
$this->user_model->insert($data);
$this->user_model->addMediaFromRequest('photo')->toMediaCollection('profile_photo');
```

### Store single file - store thumbnail

[](#store-single-file---store-thumbnail)

It's useful if you want to store the thumbnail of the file, just add withThumbnail method after addMediaFromRequest method

```
$this->user_model->insert($data);
$this->user_model->addMediaFromRequest('photo')->withThumbnail()->toMediaCollection('profile_photo');
```

### Store single File - with custom name

[](#store-single-file---with-custom-name)

only use usingFileName method after addMediaFromRequest method, this will be useful if you want to rename the file before store it to database

```
$this->user_model->insert($data);
$this->user_model->addMediaFromRequest('photo')->usingFileName('data_'.random(20))->toMediaCollection('profile_photo');
```

### Store Multi File - Different Name

[](#store-multi-file---different-name)

store 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)

```
$this->user_model->insert($data);
$this->user_model->addMediaWithRequestCollectionMapping([
      'file_input_photo' => 'profile_photo_collection',
      'file_input_profile_cover' => 'profile_photo_collection'
    ])
```

### Store Multi File - Same Name

[](#store-multi-file---same-name)

This 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

```
$this->product_model->insert($data);
$product_images = $this->request->getFiles();

    foreach ($product_images['photos'] as $key => $p) {
    $this->product_model->addMedia($p)->toMediaCollection('product_image');
    }
```

### Get Single File - Metadata

[](#get-single-file---metadata)

```
$user = $this->user_model->where('id', $id)->first();
$user->photo = $this->user_model->mediaOf('profile_photo')->getFirstMedia();

return view('user/edit', $data);
```

above will return null if no file meta information returned, handle it like this

```
