PHP Custom CMS - How to attach an Image to page template with MYSQLI

Hi, I am building a CMS with PHP and MySQL.

I have created a page template and I am able to create pages from a form in the admin area and this is populated by the Pages table in mySQL DB.
This is working great.

I also have a form in my admin area where I can upload images into my Images table which stores the image id, image title etc.
Again this works great.

Now I am trying to add an image to a page when I create a new page and I have come unstuck!

I think I understand what the logic would be, as in when I create a new page I could add the image ID into a form input and then get that image by its ID.
But as neither of my tables have a relationship (not even sure if they need a relationship) I’m struggling to understand how this would or could work.

Any ideas?

In this day and age users would much prefer the ability to drag and drop media rather than resorting to the old archaic file upload button. There is no point in creating a cms that looks like it was built over a decade ago with poor usability decisions.

But even if I had drag and drop functionality for uploading images the image would still need to be attached to the page somehow

The simplest approach that doesn’t account for any future changes would be a table that stores the page id and image file path. Additionally there could be table where a row is created for each media asset that is uploaded. In that case the file info like path, name, etc would be stored in the media table with a foreign key reference in the previous relationship table. The relationship here is pages has and belongs to many media assets.

You should also be aware that saving media assets directly on the file system has a lot of downsides. The first of which is ability to scale. Scaling an application that writes files on the same server requires duplicating all the code and media assets across all servers. A much better approach is to create a dedicated media server separate from the app server or use a cdn. This way the media assets don’t need to be copied to servers when scaling out only the app code does for php.

Furthermore, serving media assets from a dedicated media server or cdn will significantly lighten the server load vs. having the app server be responsible for serving media. There isn’t really any reason to have the media served from the app server except laziness or I guess budget/time.

Additionally, the less ability the app server has to alter the file system the more secure the app server will be. Depending on hosting environments some organizations don’t even allow apps to write to the file system for security.

I highly recommend looking at fly system cloud adaptors for various cdn options instead of managing media within the cms itself.

Using fly system you could also easily support multiple media storage solutions and wouldn’t be tied to any specific storage. So users would have the flexibility to choose what type of storage is best suited for them.

Lastly the age of php monolithic cms platforms is over. Companies that want to remain relevant and attract attention of talented individuals have embraced JavaScript for building front-end modern experiences that run completely in the browser. The dominant “cms” like platforms in this day and age are the ones that write content to static html files enabling users to easily deploy a dynamic site at no cost without a server even or a database. So I’m not sure if you are just doing this to learn php but be warned it is somewhat of a worthless (sorry I don’t know a better word) endevur. Its your time though but thought I word warn you so you aren’t surprised when no one really cares about what you have created. As harsh as that may sound. Hope I don’t get reported…

php is still relevant in the industry in the context of maintaining those monolithic cms platforms and those companies that have decided to remain loyal to php and go down with the ship. The future is hear and it is JavaScript driven experiences in the browser. You would be better served building a JavaScript cms than a php one. If you really wanted to you could still use php but as a rest api that serves data to the front-end application running in the browser. Learning about rest apis and front-end development would better serve you than php. Just go look at the job market its full of companies wanting full stack javascript devs which includes building rest apis most times.

1 Like

But don’t forget that a file upload button is still required for users unable to use a mouse, or with poor coordination who find that method easier.

4 Likes

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.