rachyharkov / codeigniter4-media
Codeigniter 4 library for managing file or media and handling upload behavior
Requires
- php: >=7.3
Requires (Dev)
This package is auto-updated.
Last update: 2025-05-04 13:48:35 UTC
README
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.
Table of Contents
- Table of Contents
- Getting Started
- How to use?
- Store single File
- Store single file - store thumbnail
- Store single File - with custom name
- Store Multi File - Different Name
- Store Multi File - Same Name
- Get Single File - Metadata
- Get Single File - Just URL
- Get All file of collection
- Query result with more data? Just assign it
- Delete file collection
- Delete and upload? Of course you can do that
- API Mode
- Roadmap
- Contributing
- License
- Contact
- Support on Trakteer
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
- PHP 7.2+
- CodeIgniter Framework (4.* recommended)
- Composer
- PHP sockets extension enabled
Installation
composer require rachyharkov/codeigniter4-media @dev
Publishing Resource
You need to publish the resources for the default configuration
php spark media:publish
Migrate
then migrate new published table
php spark migrate
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?
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
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
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 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
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
$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
<img src="<?= $user->media ? $user->media->file_path.'/'.$user->media->file_name : $user->media ?>" alt="User Photo Profile">
Get Single File - Just URL
This 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
$user = $this->user_model->where('id', $id)->first(); $user->photo = $this->user_model->mediaOf('profile_photo')->getFirstMediaUrl(); return view('user/edit', $data);
It's possible to pass parameter with 'thumb' value to get the thumbnail of the file
$media->getFirstMediaUrl('thumb');
Get All file of collection
$user = $this->user_model->where('id', $id)->first(); $user->collection_of_photo_profile = $this->user_model->mediaOf('profile_photo'); return view('user/edit', $data);
Query result with more data? Just assign it
The second parameter mediaOf()
accept id of media who owned by the record
$users = $this->users_model->findAll(); foreach($users as $key => $value) { $users[$key]->photo = $this ->ticket_log_model ->mediaOf('profile_photo', $value->id) ->getFirstMediaUrl(); }
Delete file collection
$this->product_model->where('id', $product->id)->delete(); $this->user_model->clearMediaCollection('profile_photo');
Delete and upload? Of course you can do that
Just like this after you find or update the record
$this->user_model->find($id); //or $this->user_model->update($data, $id); $this->user_model->clearMediaCollection('profile_photo'); $this->user_model->addMediaFromRequest('photo')->toMediaCollection('profile_photo');
API Mode
create 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)
🪄 Backend - Store File
public function api_upload() { $user_id = $this->request->getVar('user_id'); return $this->user_model->addMediaFromRequest('file')->toMediaCollection('profile_photo')->responseJson(); }
you will get this response
{ status: "success", message: "File uploaded successfuly", data: { collection_name: "profile_photo" file_ext: "jpg" file_name: "default" file_path: "uploads/profile_photo" file_size: 62431 file_type: "image/jpeg" model_id: "200090" model_type: "App\\Models\\User" orig_name: "20211128_165410.jpg" unique_name: "1691502324_94b5e01970c97f5ac670.jpg" } }
🪄 Backend - Delete File
On your controller, create method like this (make sure to return the responseJson method)
public function api_delete() { return $this->user_model->clearMediaCollection(request()->getVar('temp_id'), 'profile_photo')->responseJson(); }
You will get this response
{ status: "success", message: "File 1691502324_94b5e01970c97f5ac670.jpg deleted successfuly", }
Roadmap
See the open issues for a list of proposed features (and known issues).
Contributing
Contributions are what makes the open source community such an amazing place to learn, inspire, and create. Any contributions you make are greatly appreciated.
- Fork the Project
- Create your Feature Branch (
git checkout -b feature/AmazingFeature
) - Commit your Changes (
git commit -m 'Add some AmazingFeature'
) - Push to the Branch (
git push origin feature/AmazingFeature
) - Open a Pull Request
License
Distributed under the MIT License. See LICENSE
for more information.
Contact
Rachmad Nur Hayat - https://rachmad.dev - rachmadnurhayat@gmail.com
Support on Trakteer
love what i made?, please support me on Trakteer
from overseas? you can Paypal me