bookmark_borderHow can I remove a specific item from an array that is stored in a table using PHP?

This is a question posted on Stackoverflow – How can I remove a specific item from an array that is stored in a table using PHP?

The actual issue unknown and it may be related to the Database ORM being used, which I have never sighted before BUT as I spent a lil bit of time looking in to this and ended up writing an answer, I didn’t submit it as it’s not quite addressing the issue but, instead of just deleting it all I thought I’d post it here for reference.

So this was my unposted answer.

Your code appears to work in regards to the removal of the entry from the array… But the indexing will be out of order I.E a Gap will occur.

To simulate this, removing the file “stuff” and working from your code and array example.

NOTE: You can run this in its own file as it’s standalone code.

// Simulate the Values from the Database
$file_array =  ["166038569262f7799c8af00.jpg", "166038569262f7799caedab.jpg", "166038569262f7799cdc59c.jpg"];
// Simulate the File to Delete from the $file_array
$file_to_delete = "166038569262f7799c8af00.jpg";

$image_sliders = [];
$images = $file_array;

if(!empty($images)) {
    $image_sliders = $images;
    foreach ( $image_sliders as $key=>$value ) {
        echo $key. ' '.$value;
        echo '<br>';;
        if ( $value == $file_to_delete ) {
            $image_sliders = array_values($image_sliders); //Re Index the array
/** @todo  DEBUG */
echo '<pre>';
echo 'LINE: '. __LINE__.'<br>';
echo '</pre>';

/** @todo  DEBUG */
echo '<pre>';
echo 'LINE: '. __LINE__.'<br>';
echo '</pre>';  

Or using array_diff to remove an entry by value.

foreach ( $image_sliders as $value ) {
    if ( $value == $file_to_delete ) {
        $image_sliders = array_diff( $image_sliders, array( $file_to_delete ) );
        $image_sliders = array_values($image_sliders);//Re Index the array

Or you can replace the whole foreach code using array_search..

if(!empty($images)) {
    $image_sliders = $images;
    if (($key = array_search($file_to_delete, $image_sliders)) !== false) {
        $image_sliders = array_values($image_sliders);//Re Index the array

And there are other ways to achieve this but that should get you started.

I just found some “other ways” that are worth a read. Deleting an element from an array in PHP

And that is the end of that… Or is it?

bookmark_borderWordPress – How to change the Maximum Upload File Size

Out of the box, PHP defaults the Maximum Upload File Size to 2M. I.E 2M is the default setting.
Where 2M represents 2 Megabytes or 2048 Bytes  (1M = 1024 Bytes).

But what if we want to upload images that are larger than 2 Megabytes?
Well, to alter the Maximum Upload File Size in WordPress, you will need to change a setting in your php.ini file.

Where it says upload_max_filesize = 2M, you will need to alter the value of 2M.  Where 2M means 2 Megabytes or 2048 Bytes  (1M = 1024 Bytes).

So, let’s say that your or your client wants to be able to upload images that are just under 10MB. Then you would change this to 10M.

It is NOT a good idea to just make this some ridiculously large number. It’s there to protect the server from having to deal with large image uploads so please, put some thought into it.

So the Original Settings are…

; Maximum allowed size for uploaded files.
upload_max_filesize = 2M

After our changes, it becomes…

; Maximum allowed size for uploaded files.
upload_max_filesize = 10M

When you make any changes you will need to restart your webserver, for these changes to take effect!
This will depend on your OS and Webserver.

On Linux/Debian, for apache2, you can use the command

$ sudo systemctl reload apache2

On Linux/Debian, for NGINX, if you are using php7.4-fpm, you will need to restart PHP, for the changes to take effect.

$ sudo systemctl reload php7.4-fpm

BUT NGINX has a Default File Upload Size Restriction.

In NGINX, just changing the PHP upload_max_filesize setting is Not Enough as NGINX has it’s own setting for this. (See WordPress – The server cannot process the image. )

It is important to set the NGINX client_max_body_size to a value equal to or larger than the PHP upload_max_filesize setting.


bookmark_borderWordPress – The server cannot process the image.

How to Fix: The server cannot process the image. This can happen if the server is busy or does not have enough resources to complete the task. Uploading a smaller image may help. Suggested maximum size is 2560 pixels.

So you have installed WordPress and you are ready to start uploading images.

In the WordPress Dashboard, you click on the Media Entry in the Menu and get the following on your browser.

NOTE: By default WordPress sets a Maximum upload file size: 2 MB or Maximum Image File Size of 2MB ).
Actually this Maximum upload file size is determined by a PHP Setting on the server. ( See How to Increase the Maximum upload file size )

So you Click on Select Files or Drag and Drop an image which is just smaller than 2MB ( and bigger than 1MB which is a clue for later) and BOOM, you get an error message like the following.

The server cannot process the image. This can happen if the server is busy or does not have enough resources to complete the task. Uploading a smaller image may help. Suggested maximum size is 2560 pixels.

So that’s annoying! But hang on, our image is under the Maximum File Size of 2MB so what is going on?

If we attempt to upload an image that is larger than the specified Maximum File Size of 2MB, we get the expected error message of

If we attempt to upload an image that is 1.3MB, we get the unexpected error message

So What is happening?

This situation occurs when you are using NGINX as your webserver.

When you attempt to upload an image that is larger than 1MB and smaller than the Maximum upload file size: 2 MB, the server will respond with the Server Error message of:

413 Request Entity Too Large

The reason is, NGINX has a file upload size setting. By Default this setting is 1M ( That’s 1 Megabyte or 1024 bytes ). As a result, the NGINX Server returns a 413 Server Error and that results in the Server Error message.

What is the FIX?

There is a setting in NGINX called client_max_body_size.

To Apply this globally across all sites

To apply this to all sites, in the file /etc/nginx/nginx.conf,  you will need to add client_max_body_size 2M; inside the http section.

File: /etc/nginx/nginx.conf

http {
    # Default is 1M
    client_max_body_size 2M;

To Apply this on a Per Website/Domain

If you are using virtual hosts. I.E you have configured individual sites by adding configuration files in /etc/nginx/sites-available/. For Example: your-domain.conf, you will need to add client_max_body_size 2M; inside the server section.

File: /etc/nginx/sites-available/your_domain.conf

server {
    # Default is 1M
    client_max_body_size 2M;

Check Changes made to NGINX Conf files are OK!

When you make changes to any configuration files it is a good idea just to check they are ok.
This can be done with the command sudo nginx -t.

oska@oska-VirtualBox:/var/www/wordpress-dev$ sudo nginx -t
nginx: the configuration file /etc/nginx/nginx.conf syntax is ok
nginx: configuration file /etc/nginx/nginx.conf test is successful

Restart the Server

And as always, if you make any changes to your webserver, you will need to restart it.

$ sudo systemctl restart nginx

How to Increase the Image Upload File Size.

In the Case where you want to upload larger files see – WordPress – How to change the Maximum Upload File Size

System Settings

This is running on a Virtual Box running Mint Linux and NGINX with PHP 7.4.

Operating System

The operating system has no impact on this and is just mentioned for reference only.

oska@oska-VirtualBox:~$ lsb_release -a
No LSB modules are available.
Distributor ID:	Linuxmint
Description:	Linux Mint 20.3
Release:	20.3
Codename:	una

NGINX Server

This is the Culprit/Cause of this issue. Not sure if the versions are important but you’d expect this has been a setting since the early days.

oska@oska-VirtualBox:~$ nginx -v
nginx version: nginx/1.23.0

bookmark_borderCodeigniter 4 HMVC just like in Codeigniter 3 but simpler.

Ok so you have downloaded CodeIgniter 4, taken a look at it and wondered… How do I incorporate HMVC like I did in CodeIgniter 3?

Well that is exactly what I pondered once I had my fresh, new, shiny installation of CodeIgniter 4 installed on my local server.

My first thought was, “So, how do I setup HMVC on this thing”?

Well after a lot of reading of the CodeIgniter 4 users guide ( you do read that don’t you?) and some messing around, it turns out that it is rather simple.

CodeIgniter 4 has been totally re-written to fully utilise the goodness of PHP 7 but the Key that unlocks it all is “Namespaces”.

Back in CodeIgniter 3 Land, the framework does a lot of the hard thinking for you in regards to “where files, etc” lived.  Having played with CodeIgniter 4 a fair bit now, all I can say is, that was a lazy way to code. On the one hand, you didn’t have to think too hard to get things done. On the other hand, you didn’t have to think too hard ( yes they are the same point). Which resulted in you not thinking too hard.

With CodeIgniter 4, you now have to think a bit more. You have to follow some real OOP rules and do some typing. But a lecture is not needed here. You came here to see how to do this magic HMVC Thing (which might explain things a little bit better).

A basic representation of the CI 4 Folder Structure

So an out of the Box Installation of CodeIgniter 4 folder structure is something like…

  • app\
    • Config\
    • Controllers\
    • Database\
    • Filters\
    • Helpers\
    • Language\
    • Models\
    • ThirdParty
    • Views\
  • system\
    • < stuff in here >
  • public\
    • < The public Files/Folders which could be renamed to public_html>
  • writeable\
    • < stuff in here>

Now, where would you put your Modules Folder? Well there are at least 2 answers. The CodeIgniter User Guide talks about creating a new Folder Outside of app\. So we could put our Modules Folder there ( I have done this and it works).

But for reasons unknown, I opted to put it inside my app/ folder.

So we would have something like…

  • app\
    • Config\
    • Controllers\
    • Database\
    • Filters\
    • Helpers\
    • Language\
    • Models\
    • Modules\
    • ThirdParty
    • Views\

Creating an Admin Module

So let’s say we want to create an Admin Module. In our Module, we will have a Controllers and a View Folder, We will also have an Admin.php Controller, and for good measure, a View file.

  • app\
    • Modules\
      • Admin\
        • Controllers\
          • Admin.php
        • Views\
          • coming_soon_view.php

Now Here Comes the Magic

Our Admin Controller is going to look like…

namespace App\Modules\Admin\Controllers;

use App\Controllers\BaseController;

 * Class Admin
class Admin extends BaseController {

    public function index() {
      echo view('App\Modules\Admin\Views\coming_soon_view')

The very important things to note here are…

CodeIgniter has No Clue as to where our new Admin Module resides. So we have to tell it. This is where those magic keywords “namespace” and “use” save the day.

Right now, if you were to type into your URL something like and press Enter, you are going to be very sad. It won’t work.

Consider this… CodeIgniter is Expecting any Controllers to live in the Controllers Folder. The same applies with Models and Views etc.

If you had noticed, for instance, this line for showing the view…

echo view('App\Modules\Admin\Views\coming_soon_view')

Apart from the very new way to load a view using “view()” and not $this->load->view(). Did you notice the very long namespace (Kind of like a path) for the View?

As Our View is living in app/Modules/Admin/Views we have to specifically inform PHP, where to find it. So the notation used is…

App\Modules\Admin\Views\ and the view filename is coming_soon_view. So we get…
echo view(‘App\Modules\Admin\Views\coming_soon_view);

The Rule is, Specify Exactly where something resides… No MORE GUESSING! Which might be a bad thing, if you are lazy, or a great thing as it fully qualifies, documents where everything lives.

So that deals with the View. How about the Controller itself?

Notice the very very 1st Line ( well in my case it is the 2nd line…) which has the namespace command.

namespace App\Modules\Admin\Controllers;

This is declaring where in the world ( well in your project ), this Controller resides. It’s effectively a Label. You can think of it as a path, but it’s a unique location where this controller lives.

Now our Admin Class is extending the provided BaseController ( we will change this when we get to building a real project ). So we need to tell PHP where this Class lives.

HINT: Any class you use in your, in this case, Controller Class needs to be fully specified.

So we have to fully qualify what it is and where it lives.

use App\Controllers\BaseController;

How can we call our new Admin Controller from the URL?

I call these kind of Modules or Controllers, FrontEnd Modules/Controllers. FrontEnd Modules/Controllers are those that are accessed directly via the URL.

All the others (Non FrontEnd Modules/Controllers) are called by FrontEnd Controllers. These are your workhorse or task specific Modules/Controllers that are NOT Callable Directly from the URL.

Again, if you attempted to call this from the URL with something like…, you will still be sad as it will not be found. Remember, that this is NOT in the default area where CodeIgniter is expecting to find Controllers. Which is under app\Controllers by default.

So how do we rectify this? The answer is Routes.

In our app/Config/Routes.php file, we have to tell CodeIgniter where our new admin controller lives.

We can use two lines such as…

$routes->get('/admin', '\App\Modules\Admin\Controllers\Admin::index');
$routes->get('/admin/(:any)', '\App\Modules\Admin\Controllers\Admin::$1');

Note: You will need to read up about Routes in the CodeIgniter User Guide. I’ve just covering the basics here.

Line 1. covers the case where we are just calling /admin. i.e

Line 2. covers the case where are adding a method name or a method name and other parameters etc.

Note: These are both GET commands (HTTP Verbs), which is what a basic web page does when it accesses your website via a browser. It performs a GET action. Again, read the CodeIgniter Users Guide

So right now, if you followed along and tried this out, hopefully it worked for you. if not, drop me a comment and I’ll do my best to help out.

But when things do go wrong, it’s a process of asking yourself some basic questions. Questions like…
Do I have the right “namespaces” declared?
Do I have the right “use” commands?
Do I have the routes set up correctly?

PHP needs to know, where your folders/Files are located. CodeIgniter needs to know what Routes to use.

So we’ve created a FrontEnd Admin Module. This is the Admin Controller we can access directly from the URL. All of our Supporting or Workhorse, Task Specific Modules/Controllers use the very same namespace, use rules as we’ve already learned with the exception of NOT creating any Routes for them.

So in the next Post we will look at how to create and use those.


bookmark_borderBack from the Wild

Yes, this site has been in limbo for a wee while due a number of reasons I won’t bore you to death with. BUT, it’s time to get back on the horse, so to speak.

The number one reason I have this blog and the major incentive of bringing it back, is to have a place where I can plonk all of my latest and greatest discoveries AND be able to come back and view them.

For the time being, it will mainly be for coding in PHP and Codeigniter 4 (which I am now loving). CodeIgniter 4 has a lot of potential headaches for folks coming from CodeIgniter 2 or 3. It’s a completely different beast and adheres to most parts, the latest PHP standards. Plus it’s been rewritten for PHP 7 AND it uses Namespaces. Namespaces use to put a “blank stare” on my face BUT now I’ve been playing in CI4, it all makes sense, finally.

Of course I am about to embark on the 3D Printing route as soon as that hits the doorstep ( about bleeding time ).

I’m also going to get back into the ole FoamBoard Planes ( See and discover the joys of it all )

And a whole bunch of other stuff…

As I love to write stuff down, writing stuff on my PC is nice but not as accessible as it should be. I mean, I forget where stuff is. So by putting it on here, I can share it with you and I can also come back and find how I did something in the past.

I use Google a whole bunch, so instead of my reading what others have done, and I am truly grateful to all the great content writers out there, you know who you are, I thought I’d add to the collective Knowledge Pot.

Plus I need to learn to write shorter sentences… Like this one.

So let’s get the show rolling. There’s lots of stuff to do, so I’ll stop flapping my fingers on here and get to some work.