Fri, 17 May 2024


Insert File Name Array Into MySQL

By: Peter, NetArt Media
Wed, 1 August 2018
Insert File Name Array Into MySQL

I thought this would make a resourceful bit of code for the PHP Tool Box, seeing as I find many articles online on the topic of uploading multiple images and storing their names in MySQL. This isn't surprising of course, as most of us use images in one context or another when it comes to web applications and websites.

I will only cover the Prepared INSERT Statement and the HTML form input statement used to carry out the upload and insertion of the image name array in this article, as there is much more coming up in the near future to assist you in creating a full-fledged script.

For those who may not know how to upload multiple images in an HTML form, here is the input code to be used for the process.


When using this HTML input code to upload multiple images make sure you are using the correct enctype function in your form. As seen below.


Below is the Prepared Statement used to process our images upload. I will dissect sections of the code for you to understand it better.


1. We prepare to insert the form field data we have entered into the HTML form using a Prepared Statement.

2. '$files = $_FILES['files'];' is how we represent our HTML form field name and then we create an array entitled $uploaded with the uploaded items.

3. As I have explained in the past about arrays, we need to explode first before doing anything with them. Especially when we are inserting an array into a MySQL database table. The foreach statement takes care of this part of the process for us. Our goal is to insert the image names into the database in a comma separated list.

4. After making sure the file names have been turned into lowercase letters we upload them and turn them into the final array. After imploding the array into the comma separated list 'bind_param' checks the files and, along with our strings, enters the information into the database table with the execute(); statement.

Here is what the array of image file names will look like in the MySQL table once this has been processed.

Make sure you configure the 'pics' column in the table for LONGTEXT, this way you can insert as large an array as is needed without MySQL truncating it due to there not being enough characters.

This code is bare bones, as in a real situation we would want to check for the proper file extensions when uploading, etc. I will cover that in the next video tutorial, which I am hoping will be tomorrow.

In the meantime, sink your teeth into this and give it a try on a development server if you have one. For those who do not, I will probably create a tutorial on installing WAMP pretty soon. So stay tuned for that.


Category: Web Development
Share this post:



See All Scripts






Subscribe for our newsletter

Receive the latest blog posts direct in your mailbox